write several functions in C++ that take a string that the user inputs and test/
ID: 667609 • 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.
Explanation / Answer
1) Prompt the user to enter a string at the console.
First we print the string in a well-known way in order to see the difference with the other approach. The second printing is the formatting Write(…) and the first argument is the format string. In this case {0} means to put the first argument after the formatting string in the place of {0}. The expression {0} is called a placeholder, i.e. a place that will be replaced by a specific value while printing.
The next example will further explain this concept:
string name = "John";
int age = 18;
string town = "Seattle";
Console.Write(
"{0} is {1} years old from {2}! ", name, age, town);
The result of this example execution is as follows:
John is 18 years old from Seattle!
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.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,len,flag=1;
char a[20];
cout<<“Enter a string:”;
cin>>a;
for(len=0;a[len]!=’’;++len);
for(i=0,j=len-1;i<len/2;++i,–j)
{
if(a[j]!=a[i])
flag=0;
}
if(flag==1)
cout<<“nThe string is Palindrome”;
else
cout<<“nThe string is not Palindrome”;
getch();
}
Output: Enter a string : Sawas
The string is Palindrome.
5) Calculate and output the length of the string.
Output: Enter a string: Programiz
Length of string: 9
6) Output only the first half of the string that was input.
7) Output only the first character of the string that was input.
string name = "John";
int age = 18;
string town = "Seattle";
Console.Write(
"{0} is {1} years old from {2}! ", name, age, town);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.