By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 962 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

question with strings

P: 57
I am writting a program that gets a line of input from a user and puts that in a string. Then it needs to correct whitespace and capitalization. If there is more then one space of whitespace between words change it to one space and if it is a period add a space.

this is what i have so far and Im trying to get the whitespace right.


Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. int main() {
  6.  
  7.         cout << "enter sentence and hit enter";
  8.         string  s1;
  9.  
  10.         getline(cin,s1);
  11.  
  12.          if(s1 != " ");
  13.         {
  14.  
  15.                 char seporator = ' ';
  16.                 if (s1[s1.length()-1] == '.')
  17.                 { seporator = '\n';
  18.                 }
  19.  
  20.                 cout << s1 << seporator;
  21.                 }
  22.  
  23.         while(s1 != " ");
  24.  
  25.         return(0);
  26.  
  27. }
Nov 8 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
You have you loop wrong, you need to be looping down the string checking each character. For each character you will probably need to set some flags, was it a space?, do you need to capital ise the next character.

Then as you read each character you can use the flags to take appropriate action

Expand|Select|Wrap|Line Numbers
  1. LastCharacterWasSpace = false
  2. LastCharacterRequiresSpace = false
  3. CapitaliseNextNonSpace = true
  4.  
  5.  
  6. FOREACH Character IN String
  7.     IF Character Is Space
  8.         IF LastCharacterWasSpace = true
  9.             REMOVE Character FROM String
  10.         ELSE
  11.             LastCharacterWasSpace = true
  12.             LastCharacterRequiresSpace = false
  13.         ENDIF
  14.     ELSE // Character is not space
  15.         IF LastCharacterRequiresSpace
  16.             INSERT Space INTO String
  17.             LastCharacterRequiresSpace = false
  18.         ENDIF
  19.  
  20.         LastCharacterWasSpace = false
  21.  
  22.         IF Character = FullStop
  23.             LastCharacterRequiresSpace = true
  24.             CapitaliseNextNonSpace = true
  25.         ELSE IF Character = Comma
  26.             LastCharacterRequiresSpace = true
  27.         ELSE
  28.             IF CapitaliseNextNonSpace = true
  29.                 Character = UpperCase( Character )
  30.             ELSE
  31.                 Character = LowerCase( Character )
  32.             ENDIF
  33.             CapitaliseNextNonSpace = false
  34.         ENDIF
  35.     ENDIF
  36. ENDFOR
  37.  
Nov 8 '06 #2

Post your reply

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