I had an assignment where I had to delete the spaces at the begining of a word using pointers, modify the original word and return a modified word. The only thing I am given is that the function name/arguments has to look like this:
char* trim(char *str)
and that in the main function must be something like this: (comments show the expected output)
char str[] = " Hello cpp", *newstr;
cout << "start" << str << "end" << endl; // start Hello cppend
newstr = trim(str);
cout << "start" << str << "end" << endl; // startHello cppend
cout << "start" << newstr << "end" << endl; // startHello cppend
Function I created looks like this:
char* trim(char *str){
while(*str<33){
str++;
}
cout<<str<<endl;
return str;
}
The idea was that when the value of the current pointer is a space(*str < 33) I want to move the address of a pointer to a next char. I used "while" so it's moving to a next place until it's pointing to a value that's not a space.
When it comes to the return everything works well. "newstr" is returned without spaces and the output is "startHello cppend" and when I output the str in the function it shows the text without spaces.
But the original value stays untouched. The output in main is still start Hello cppend
Why is it that when I move the pointer in the function using str++ the original pointer doesn't change and in the main function it's still with spaces but the str in the function is shown without spaces?