Monday, July 27, 2009

Removing leading spaces from a string C++?

I need to remove the leading spaces in any line input so that the output of this program displays the string without a leading space. I have come up with the following code but it only outputs the first letter of the first word and not the whole string:





int main(int argc, char **argv)


{


AnsiString Line;


int Index;





Line = ReadStringPr("Enter your text. It must not finish with a space character." );


Index = 1;


//read each char of Line





while ((Index %26lt;= Length(Line) %26amp;%26amp; Line[Index] == ' '))


Index = Index + 1;


{


//if current char is not a space


if (Line[Index] != ' ')//;


{


//write current char to screen


WriteChar(Line[Index]);


Index = Index + 1;


}


else //if current char is a space


{


//to place a space after each word is written


WriteChar(' ');





//ignore all consecutive spaces


while (Line[Index] == ' ')//;


Index = Index + 1;


}


}


getchar(); // to keep display on until you print


return 0;


}





Help pls!

Removing leading spaces from a string C++?
I see a couple of problems.





You are starting the loop with index set to 1, arrays are 0 based, so you're skipping over the first character.





The big problem, though, is that your loop condition:





while ((Index %26lt;= Length(Line) %26amp;%26amp; Line[Index] == ' '))





basically says stop looping when you get to the first non-space character. Since you are outputting one character on each iteration, that means you are going to stop


prematurely. If you want to maintain the structure of your program, then you should take the Line[Index]==' ' clause out of the while loop condition.





Now, inside your loop, the code seems to say, "if the character is not a space, write it out, if it is a space, write a space, and then skip over consecutive spaces. That's not going to suppress leading spaces, is it? It is just going to turn many spaces into one space anywhere it encounters them.





If you put your loop for detecting continuous spaces before the main loop, you could advance the index (if you started it with 0) until it was past all the initial spaces. Then you could just output the rest of the string, one character at a time, if you really wanted to, without looking for spaces inside the string at all, or keeping the business about skipping consecutive spaces if that's really something you want to do.





If the consecutive spaces inside the string isn't really an issue, then I probably would have advanced a character pointer through the string until I got to the first nonspace, and then I'd have a pointer to a string that doesn't start with spaces, and could output it all in a single string output statement.





Or you could just use a library function to trim the string, but I assume you are doing this as a programming exercise.





Good luck.
Reply:http://pegasus.rutgers.edu/~elflord/cpp/...





#include %26lt;string%26gt;





Much easier to work with.


No comments:

Post a Comment