473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Remove extra blanks from a string

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

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char **argv)
  2. {
  3.  AnsiString Line;
  4.  int Index;
  5.  
  6. Line = ReadStringPr("Enter your text. It must not finish with a space character." );
  7. Index = 1;
  8. //read each char of Line
  9.  
  10. while (Index <= Length(Line))
  11. {
  12.    //if current char is not a space
  13.    if (Line[Index] != ' ')//;
  14.       {
  15.       //write current char to screen
  16.       WriteChar(Line[Index]);
  17.       Index = Index + 1;
  18.       }
  19.    else //if current char is a space
  20.       {
  21.             WriteStringCr(' ');
  22.       }
  23.       //ignore all consecutive spaces
  24.       while (Line[Index] == ' ')//;  this semicolon may have created an infinite loop not allowing anything to print to screen
  25.         Index = Index + 1;
  26.  
  27.  getchar();  // to keep the display on until you press Enter key
  28.  return 0;
  29.  }
Mar 21 '07 #1
3 4023
horace1
1,510 Expert 1GB
have you a } in the wrong place?
Expand|Select|Wrap|Line Numbers
  1. else //if current char is a space
  2. {
  3. WriteStringCr(' ');
  4. }  <<< should this
  5. //ignore all consecutive spaces
  6. while (Line[Index] == ' ')//; this semicolon may have created an infinite loop not allowing anything to print to screen
  7. Index = Index + 1;
  8. <<  be here????
  9.  
Mar 21 '07 #2
Unfortunately moving this brace does not change anything. The words are still written one on each line and not as a corrected sentence. Braces seem to be the root of all evils!!
Mar 21 '07 #3
Ganon11
3,652 Expert 2GB
What is the difference between WriteChar() and WriteStringCr()? Does one of them always output a newline character after execution?
Mar 21 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: revjjjames | last post by:
Hello - What is the correct expression to remove any extra line breaks beyond two in a string? Sincerely, Jim
3
by: Jade | last post by:
hi I want the empty spaces to be removed after a string has been entered by a user i.e how do i get rid of the trailing blanks in the following string.... 'John So that the value now displays...
1
by: John | last post by:
I have a SQL Query (SQL2005) returns for XML statement, my client is using ExecuteXmlReader to execute the query, everything if fine until I use ReadOutXML, the string has a extra \ before every ",...
10
by: Hermann.Richter | last post by:
Is there any way to remove automatically all blank chars from the html sent to the client?? Example: ---------------------------------------------------------------- <? echo "HTML tags...
34
by: Registered User | last post by:
Hi experts, I'm trying to write a program that replaces two or more consecutive blanks in a string by a single blank. Here's what I did: #include <stdio.h> #include <string.h> #define MAX 80
0
by: yongsheng | last post by:
Hi, i've searched through the discussions but haven't got a clue to solve my problem. Im new to VBA, maybe its a simple task but any help is welcomed. The Problem: Every month, I need to sort...
4
by: subrahmanyaam | last post by:
Guys, I have a textbox1.text on my form. If any person enters the name as D'Souza, i want to eliminate that " ' " from the text box before i could save it. How do i do this... please comment I...
10
by: Diego F. | last post by:
Hi all. I have an application that receives a message from a socket in an array from a certain size. As the array size may be longer that the message received, the end of the array has blank...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.