0

I'm trying to add a space behind capital letters of a string for example. "ILikeBananas" would turn into "I like bananas"

I've tried using a while loop, isupper, and .insert((i-1), " "). I noticed that it would work if it were i+1 however that would give me the wrong output.

void fixedInput(string &userInput) {
int i = 1;
while (userInput[i]) {
    if (isupper(userInput[i])) {
        userInput.insert((i-1)," ");
        tolower(userInput[i]);
    }
    i++;
}
}   

with (i-1) there is no output

Brett
  • 41
  • 6
  • `userinput[1]` is not the 1st character of the string. Furthermore, a `std::string` is not terminated with a null character. You are mixing up C++ `std::string`s and C-style null-terminated strings. And once you insert something before `userInput[i]`, then, obviously, `userInput[i]` will not be what it was before. Once you understand all three of these problems, you should be able to fix this. – Sam Varshavchik May 24 '19 at 02:18

1 Answers1

0

Take a look at https://stackoverflow.com/a/14494432/11397643

Using that concept you should be able to append a space as well when doing a conversion

Narsarius
  • 55
  • 1
  • 7