In: Computer Science
Create a program that accepts in a string of 2 or more words. The program then copies the entered string changing the alpha characters into digits representing characters with acsenders, descenders and nonascender/desender characters; uppercase characters should be treated as lower case. The characters with descenders (gjpqy) should be replaced with a 1. The characters with ascenders (dbfhklt) should be replaced with a 2. The rest of the alpha characters should be replaced with a 3. The converted string should then be displayed. The program should also prompt the user if they would like to enter another string. If they answer y or Y, loop back to the beginning of the program. If they answer n or N, exit the program. All user input should be validated. The word string should have at least one space in it. The Yes or No input must be y, Y, n or N. Do not create an SDM for this lab.
Output Layout:
Please enter a string of at least 2 words: Fred
Please enter a string of at least 2 words: Garrett Phelps
Garrett Phelps
1333322 123213
Would you like to enter another string (y or n): q
Would you like to enter another string (y or n): y
Please enter a string of at least 2 words: I'm Doug
I'm Doug
3'3 2331
Would you like to enter another string (y or n): n
bye!
8. Other:
Try replacing the "123" as shown below
'1' can use static_cast(220) '2' can use static_cast(223) '3' can use static_cast(254)
#include <iostream>
using namespace std;
bool twoWord(string str);
string changer(string str);
intmain(){
string str1 , str2;
char c ;
while(true){
while(true){
cout<<"Please enter a string of atleast 2 words:"<<endl;
cin>>str1>>str2;
cout<<changer(str1)<<" "<<changer(str2)<<endl;
break;
}
cout<<"would you like to enter another string(y or n)"<<endl; //asking if a user wants to convert more strings.
cin>>c;
if(c == 'n' || c == 'N'){
cout<<"bye";
break;
}
}
}
bool twoWord(string str){ //checking if the words in string are atleast 2.
for(inti = 0 ; i<str.size() ; i++){
if(str[i] == ' '){
return true;
}
}
return false;
}
string changer(string str){// changing string of with accenders and descenders.
for(inti = 0 ; i<str.size() ; i++){
if(str[i] >= 'A' &&str[i] <= 'z' ){
if(str[i] == 'g' || str[i] == 'j' || str[i] == 'p' || str[i] == 'q' || str[i] == 'y'){
str[i] = '1';
}
else if(str[i] == 'd' || str[i] == 'b' || str[i] == 'f' || str[i] == 'h' || str[i] == 'k' || str[i] == 'l'|| str[i] == 't' ){
str[i] = '2';
}
else {
str[i]='3';
}
}
}
return str;
}
COMMENT DOWN BELOW FOR ANY QUERIES AND,
LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.