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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gdarian216
    New Member
    • Oct 2006
    • 57

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

    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.
  • gdarian216
    New Member
    • Oct 2006
    • 57

    #2
    Originally posted by gdarian216
    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.
    Code:
    #include<string>
    #include<iostream>
    #include<cctype>
    using namespace std;
    
    int main() { 
              
            cout << "enter sentence and hit enter";
            string  s1; 
             
            getline(cin,s1);
    
            s1[0] = toupper(s1[0]);
            for(int i = 1; i<s1.length(); i++)
            {
            s1[i] = tolower(s1[i]);
            }
            string s2;
            int index = 0;
            s2 = "";
            for(int i=0; i<s1.length(); i++)
            {
            if (s1[i] != ' ')
            s2[index] = s1[i];
            index++;
            }
            cout << s2 << endl;
    
    }
    this is what i have so far

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      Hi,
      There were few problem in the code...
      This one works fine
      Code:
      #include<string>
      #include<iostream>
      #include<cctype>
      using namespace std;
      
      int main() { 
                
              cout << "enter sentence and hit enter"<<endl;
              string  s1; 
               
              getline(cin,s1);
      		cout<<"OUT is :"<<s1<<endl;
      
              s1[0] = toupper(s1[0]);
              for(int i = 1; i<s1.length(); i++)
              {
      			s1[i] = tolower(s1[i]);
              }
      		cout<<"OUT is :"<<s1<<endl;
              string s2(s1.length(),' ');
      		//string s2;
              int index = 0;
              //s2 = "";
              for(int i=0; i<s1.length(); i++)
              {
      			if (s1[i] != ' ')
      			{
      				s2[index++] = s1[i];
      			}
      			//index++;
             }
      		cout<<"S2 is :"<<s2<< endl;
      
      }
      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

      Comment

      Working...