I'm writing a function in C (not C++, this is going to run on an older computer), which should take an input char* and add spaces to it, based on letter capitalization and numbers, then return the result. I cannot use strings and their functions I'm afraid, due to platform limitations.
For example, the input "TestingThisPieceOfText" should be returned as "Testing This Piece Of Text".
I have some (rather crude for now) code that works for simple cases like this, but I would like to add some exceptions to the rule, and that's where I need help:
- If multiple capital letters are in a sequence, they should not be separated by spaces. For example, "APB" should stay as-is.
- If there are numbers in the input string, there should be a space before (and after), but not between them. For example, "A10TankKiller2Disk" should be returned as "A10 Tank Killer 2 Disk".
- Special-case for "Mc", to cover cases where such names might be sent in. For example, "ScroogeMcDuckIsFilthyRich" should be returned as "Scroodge McDuck Is Filthy Rich".
Here's the function as it currently stands (like I said, a little crude for now):
char* add_spaces_to_string(const char* input)
{
char input_string[100];
strcpy(input_string, input);
char* output = (char*)malloc(sizeof input_string * 2);
const char capitals[] = "ABCDEFGHIJKLMOPQRSTVWXYZ";
const char numbers[] = "1234567890";
const char mc[] = "Mc";
// Special case for the first character, we don't touch it
output[0] = input_string[0];
unsigned int output_index = 1;
unsigned int capital_found = 0;
unsigned int number_found = 0;
for (unsigned int input_index = 1; input_string[input_index] != '\0'; input_index++)
{
for (int capitals_index = 0; capitals[capitals_index] != '\0'; capitals_index++)
{
if (capitals[capitals_index] == input_string[input_index]
&& capital_found < input_index - 1
&& number_found < input_index - 1)
{
capital_found = input_index;
//printf("Found a capital character (%c), in position %u. Adding a space.\n", input_string[i], i);
output[output_index] = ' ';
output_index++;
output[output_index] = input_string[input_index];
}
}
for (int numbers_index = 0; numbers[numbers_index] != '\0'; numbers_index++)
{
if (numbers[numbers_index] == input_string[input_index]
&& capital_found < input_index - 1
&& number_found < input_index - 1)
{
number_found = input_index;
output[output_index] = ' ';
output_index++;
output[output_index] = input_string[input_index];
}
}
output[output_index] = input_string[input_index];
output_index++;
}
output[output_index] = '\0';
return output;
}
With the above, simple examples like
"AnotherPieceOfTextWithoutSpaces"
are correctly converted to
"Another Piece Of Text Without Spaces"
but more complex ones, like
"A10TankKiller2Disk"
are not - it returns
"A1 0Tank Killer 2Disk"
in that case.
So the question is, why am I getting spaces in the positions I don't want, while not getting them where I want (Based on the rules I mentioned above)?
Any pointers to the right direction would be greatly appreciated! :)