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

question on how to make a program to read and correct syntax

P: 57
I am tring to write a program that will take a string of text input and correct the spacing and capitalization. I have been able to get it to except the string by using getline. I know that the easest way to do it would be to make everything lowercase then make loop to look for whitespace and make sure there is only one space between words, commas, and periods. I then would need another loop to make every letter after a period capital and the first letter of the string. I cant figure out how to do the loop to remove and whitespace. If anyone can help I would appreciate it.
Apr 5 '07 #1
Share this Question
Share on Google+
2 Replies


P: 57
I am tring to write a program that will take a string of text input and correct the spacing and capitalization. I have been able to get it to except the string by using getline. I know that the easest way to do it would be to make everything lowercase then make loop to look for whitespace and make sure there is only one space between words, commas, and periods. I then would need another loop to make every letter after a period capital and the first letter of the string. I cant figure out how to do the loop to remove and whitespace. If anyone can help I would appreciate it.
Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3. #include<cctype>
  4. using namespace std;
  5.  
  6. int main() { 
  7.  
  8.         cout << "enter sentence and hit enter";
  9.         string  s1; 
  10.  
  11.         getline(cin,s1);
  12.  
  13.         s1[0] = toupper(s1[0]);
  14.         for(int i = 1; i<s1.length(); i++)
  15.         {
  16.         s1[i] = tolower(s1[i]);
  17.         }
  18.         string s2;
  19.         int index = 0;
  20.         s2 = "";
  21.         for(int i=0; i<s1.length(); i++)
  22.         {
  23.         if (s1[i] != ' ')
  24.         s2[index] = s1[i];
  25.         index++;
  26.         }
  27.         cout << s2 << endl;
  28.  
  29. }
  30.  
this is what i have so far
Apr 6 '07 #2

gpraghuram
Expert 100+
P: 1,275
Hi,
There were few problem in the code...
This one works fine
Expand|Select|Wrap|Line Numbers
  1. #include<string>
  2. #include<iostream>
  3. #include<cctype>
  4. using namespace std;
  5.  
  6. int main() { 
  7.  
  8.         cout << "enter sentence and hit enter"<<endl;
  9.         string  s1; 
  10.  
  11.         getline(cin,s1);
  12.         cout<<"OUT is :"<<s1<<endl;
  13.  
  14.         s1[0] = toupper(s1[0]);
  15.         for(int i = 1; i<s1.length(); i++)
  16.         {
  17.             s1[i] = tolower(s1[i]);
  18.         }
  19.         cout<<"OUT is :"<<s1<<endl;
  20.         string s2(s1.length(),' ');
  21.         //string s2;
  22.         int index = 0;
  23.         //s2 = "";
  24.         for(int i=0; i<s1.length(); i++)
  25.         {
  26.             if (s1[i] != ' ')
  27.             {
  28.                 s2[index++] = s1[i];
  29.             }
  30.             //index++;
  31.        }
  32.         cout<<"S2 is :"<<s2<< endl;
  33.  
  34. }
  35.  
Problem is if u declare a string like this
string s2("");
and then put element into the string like an array (s2[i]='s';)
then it is a array overwrite.

Thanks
Raghuram
Apr 6 '07 #3

Post your reply

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