473,698 Members | 2,873 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Remove extra blanks from a string

2 New Member
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 4058
horace1
1,510 Recognized Expert Top Contributor
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
afr02hrs
2 New Member
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 Recognized Expert Specialist
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
3223
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
296
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 'John Any help is appreciated.
1
1119
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 ", how do I remove it? Here is the xml result I got <SearchResults xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> <SearchResult id=\"2B12DD88-B472-449A-8A69-62563174A28A\"> <title> testTitle </title> </SearchResult>
10
1669
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 here<br>" ; ?> more HTML here <div>
34
4101
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
1310
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 data in an excel sheet where each set of data is submitted in 3 rows. In these rows, some cells are merged and some are not. As such, i get empty blank rows in between. My aim is to remove all these blanks by deleting the whole row. Data in merge...
4
1595
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 am able to detect that extra character but dont know how to replace the text box without string (OR) removing the string from that name. The code to trace the string is as below: dim str as string str=textbox1.text if str.contains( " ' " ) then...
10
2806
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 characters. I use the System.Text.Encoding.ASCII.GetString method to convert to string and insert in a database. The problem is that in the database the blanks are inserted as well. How can I remove the blanks before inserting in the database?
0
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4624
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.