Create a CharConverter class that performs various operations on strings. It sho
ID: 3858460 • Letter: C
Question
Create a CharConverter class that performs various operations on strings. It should have the following two public member functions. The uppercase member function accepts a string and returns a copy of it with all lowercase letters converted to uppercase. If a character is already uppercase or is not a letter, it should be left alone. The properwords member function accepts a string of words seperated by spaces and returns a copy of it with the first letter of each word converted to uppercase. Write a simple program that uses the class. It should prompt the user to input a string. Then it should call the properwords function and display the resulting string. Finally, it should call the uppercase function and display this resulting string. The program should loop to allow additional strings to be converted and displayed until the user chooses to quit. The language is C++
Explanation / Answer
We have used the ASCII values in order to convert lower case to upper case.
#include <iostream>
#include <string>
using namespace std;
class CharConverter{
public:
CharConverter();
string uppercase(string);
string properWords(string);
private:
};
CharConverter::CharConverter(){
}
string CharConverter::uppercase(string str){
string newString;
for(int i = 0; i < str.size(); i++){
if(str[i] >= 97 && str[i] < 123){
str[i] = str[i] - 32;
}
newString += str[i];
}
return newString;
}
string CharConverter::properWords(string str){
string newString;
int index=0;
for(int i = 0; i < str.size(); i++){
if(str[i] >= 97 && str[i] < 123) {
if(i==0){
str[i] = str[i] - 32;
}
if ( str[i - 1] == ' ' ){
str[i] = toupper( str[i] );
}
}
newString += str[i];
}
return newString;
}
int main(){
CharConverter reg;
string word, pWord, uWord;
cout << " Enter a word to be converted: ";
getline(cin,word);
pWord = reg.properWords(word);
uWord = reg.uppercase(word);
cout << " " << "Proper Word Is : " << pWord;
cout << " " << "UPPERCASE IS : " << uWord;
return 0;
}