Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

write several functions in C++ that take a string that the user inputs and test/

ID: 668051 • 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!) . 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>
#include <algorithm>
using namespace std;
void reverse(string x){

reverse(x.begin(), x.end());
cout << "reverse = " << x << endl;

}
bool isPalindrome(const string& x){

if (x.empty())
return false;

int i = 0; // first characters
int j = x.length() - 1; // last character

while (i < j)
{
if (x[i] != x[j])
{
return false;
}
i++;
j--;
}
return true;
}
void len(string x){
cout<<"size of the string:"<<x.length()<<" ";
}
void firstHalf(string x){
int s=x.length();
int half=s/2;
string str=x.substr(0,half);
cout<<"first half of the string:"<<str<<" ";
}
void firstLetter(string x){
int s=x.length();
  
string str=x.substr(0,1);
cout<<"first letter of the string:"<<str<<" ";
}
int main()

{

string x ;
cout<<"Enter the string to : ";
cin>>x;
cout<<"your entered string:"<<x<<" ";
reverse(x);
if(isPalindrome(x)){
cout<<"is a palindrome ";
}else{
cout<<"not a palindrome ";
}
len(x);
firstHalf(x);
firstLetter(x);
return 0;
}