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

Question about strings

P: 43
Im writing a progrem that takes input (a sentence) from the user and I about it in the proper format. All i need now is to figure out how to get the spacing correct in the sentence. I need all the strings of two or more blank spaces should be compressed to one single space. Here is what I got so far. Any ideas on how to go about doing the spacing would be greatly appreciated. Thanks!!

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string str;
cout << "Enter your sentence and then press enter. " << endl;
getline (cin, str);
str[0] = toupper (str[0]);
for (int i = 1; i < str.length(); i++)
{
str[i] = tolower (str[i]);
}
cout << str << endl;

}
Nov 12 '06 #1
Share this Question
Share on Google+
3 Replies


Banfa
Expert Mod 5K+
P: 8,916
You can do this in 1 variable but I think it is easier (and requires less copying of data to do in in 2.

Expand|Select|Wrap|Line Numbers
  1. Initialise result string to ""
  2. Get input string
  3.  
  4. For each character in the input string
  5.     If it is not space or the previous character was not space
  6.         Copy character to the result string
  7.     End If
  8. End For
  9.  
Nov 13 '06 #2

P: 43
You can do this in 1 variable but I think it is easier (and requires less copying of data to do in in 2.

Expand|Select|Wrap|Line Numbers
  1. Initialise result string to ""
  2. Get input string
  3.  
  4. For each character in the input string
  5.     If it is not space or the previous character was not space
  6.         Copy character to the result string
  7.     End If
  8. End For
  9.  
ALright i tried to do what you said but I'm a begginner programmer and Im not sure how to do some of the things you said: here's what i got now:

string result;
string str;
cout << "Enter your sentence and then press enter. " << endl;
getline (cin, str);
str[0] = toupper (str[0]);
for (int i = 1; i < str.length(); i++)
{
str[i] = tolower (str[i]);
}
result = "";
i = 0;
for (int i = 0; i < str.length(); i++)
if (str[i] != " " || str[i-1] != " ")
return = result;

cout << str << endl;

Im getting errors witht hat...I don't know how to copy character to result string or enter in if not a space i just guessed to use " "....Please help
Nov 13 '06 #3

Expert 100+
P: 1,510
you have a couple of errors indicated by // ** comments
Expand|Select|Wrap|Line Numbers
  1.     string result;
  2. string str;
  3. cout << "Enter your sentence and then press enter. " << endl;
  4. getline (cin, str);
  5. str[0] = toupper (str[0]);
  6. for (int i = 1; i < str.length(); i++)
  7. {
  8. str[i] = tolower (str[i]);
  9. }
  10. result = "";
  11. //i = 0;                               // ** removed
  12. for (int i = 0; i < str.length(); i++)
  13.    if (str[i] != ' ' || str[i-1] != ' ') // ** replaced " with '
  14.      return = result;
  15. cout << str << endl;
  16.  
you now need to replace the line
Expand|Select|Wrap|Line Numbers
  1. return = result;
  2.  
with code which adds the character from str[i] to result
Nov 13 '06 #4

Post your reply

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