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.
question on how to make a program to read and correct syntax
Collapse
X
-
Tags: None
-
Originally posted by gdarian216I 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.this is what i have so farCode:#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; } -
Hi,
There were few problem in the code...
This one works fine
Problem is if u declare a string like thisCode:#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; }
string s2("");
and then put element into the string like an array (s2[i]='s';)
then it is a array overwrite.
Thanks
RaghuramComment
Comment