Hi Experts
I am trying to get the following program running correctly. I would like to be able to input a line of text containing multiple space characters between words and have the program remove any number of excess space characters so that the sentence is rewritten correctly containing only 1 space character between individual words. However this program outputs each word on a separate line! Is there a problem with my loop?
Many thanks
I am trying to get the following program running correctly. I would like to be able to input a line of text containing multiple space characters between words and have the program remove any number of excess space characters so that the sentence is rewritten correctly containing only 1 space character between individual words. However this program outputs each word on a separate line! Is there a problem with my loop?
Many thanks
Code:
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 <= Length(Line)) { //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 { WriteStringCr(' '); } //ignore all consecutive spaces while (Line[Index] == ' ')//; this semicolon may have created an infinite loop not allowing anything to print to screen Index = Index + 1; } getchar(); // to keep the display on until you press Enter key return 0; }
Comment