write several functions in C++ that take a string that the user inputs and test/
ID: 667939 • 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 <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {''};
int i, length = 0, flag = 0;
printf("Enter a string ");
gets(string);
for (i = 0; string[i] != ''; i++)
{
length++;
}
printf("The length of the string '%s' = %d ", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome ", string);
else
printf("%s is not a palindrome ", string);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.