write several functions in C++ that take a string that the user inputs and test/
ID: 667952 • Letter: W
Question
write several functions in C++ that take a string that the user inputs and test/manipulate it. Your program must: 1. Prompt the user to enter a string at the console. 2. Output the original string that was just input by the user. 3. Output the reverse of the string that was input. 4. Determine if the word is palindrome (Google it if you don’t know what it is!) and output a statement indicating if it is or is not a palindrome. 5. Calculate and output the length of the string. 6. Output only the first half of the string that was input. 7. Output only the first character of the string that was input. Use cout and cin for the output and input (remember to include iostream!) • You will use the string class for this program. The program will repeat until a empty line is entered as input, at which time it will exit.
Sample Output
Please enter your word: universe
The word universe backwards is esrevinu
This word is not a palindrome.
The word is 8 characters long.
The first half of the word is univ
The first letter is u
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
void revWord(string str)
{
cout<<"The word "<<str<<" backwards is "<<string(str.rbegin(), str.rend() )<<" ";
}
void isPalin(string str)
{
if(str == string(str.rbegin(), str.rend() ))
{
cout<<"This word is palindrome ";
}
else {
cout<<"This word is not palindrome ";
}
}
void lenWord(string str)
{
cout<<"The word is "<<str.length()<<" characters long ";
}
void fhalfWord(string str)
{
cout<<"The first half of the word is "<<str.substr(0,str.length()/2)<<" ";
}
void fletterWord(string str)
{
cout<<"The first letter is "<<str.substr(0,1)<<" ";
}
int main()
{
string word;
int flag = 1;
while(flag)
{
cout << "Please enter your word : "; getline(cin, word);
if(word.length()>0)
{
revWord(word);
isPalin(word);
lenWord(word);
fhalfWord(word);
fletterWord(word);
cout<<endl;
}
else flag=0;
}
return 0;
}
The above program has 5 functions and it behaves exactly the same as shown in Sample Output. The code is quite clear. Start reading the code from the main It will be easy to understand. Let me know if you want some explanation OR any other help.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.