I have a question regarding C++. This is my current function:
string clarifyWord(string str) {
//Remove all spaces before string
unsigned long i = 0;
int currentASCII = 0;
while (i < str.length()) {
currentASCII = int(str[i]);
if (currentASCII == 32) {
str.erase(i);
i++;
continue;
} else {
break;
}
}
//Remove all spaces after string
i = str.length();
while (i > -1) {
currentASCII = int(str[i]);
if (currentASCII == 32) {
str.erase(i);
i--;
continue;
} else {
break;
}
}
return str;
}
Just to get the basic and obvious things out of the way, I have #include <string>
and using namespace std;
so I do have access to the string functions.
The thing is though that the loop is quitting and sometimes skipping the second loop. I am passing in the str
to be " Cheese "
and it should remove all the spaces before the string and after the string.
In the main function, I am also assigning a variable to clarifyWord(str)
where str
is above. It doesn't seem to print that out either using cout << str;
.
Is there something I am missing with printing out strings or looping with strings? Also ASCII code 32
is Space
.