In: Computer Science
Takes a string and removes all spaces and special characters,
and converts
// to upper case, returning the result
// Pre: inString contains a string
// Post: Returns a string with all spaces and special characters
removed and
// converted to upper case
in c++ please
#include <iostream>
#include <iomanip>
using namespace std;
string convertString(string s){
string t = "";
for(int i=0; i<s.length(); i++){
if(s[i] >= 'a' && s[i]<= 'z'){
t = t + (char)(s[i]-32);
}
else if(s[i] >= 'A' && s[i]<= 'Z'){
t = t + s[i];
}
else if(s[i] >= '0' && s[i]<= '9'){
t = t + s[i];
}
}
return t;
}
int main()
{
string s;
cout<<"Enter the string: ";
getline(cin , s);
cout<<"New string is:
"<<convertString(s)<<endl;
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Enter the string: suresh murApaka cse12!@ hi how Are y123
New string is: SURESHMURAPAKACSE12HIHOWAREY123