C++ Help Write a C++ program that accepts a string from the user and then displa
ID: 3812339 • Letter: C
Question
C++ HelpWrite a C++ program that accepts a string from the user and then displays the string in several different formats using a different function to accomplish each task listed below: a. Reads and displays a string entered by the user. b. Calculates and displays the length of the string. c. Displays the total number of words that appear in the string. d. Display the string backwards. Sample I/O: Enter message: I cannot wait for summer The message you entered was: I cannot wait for summer The length of the string entered is: 24 Number of words in message: 5 The message displayed backward is: remmus rof tiaw tonnac I Run program again? Yes = 1 or No = 0 1 Enter message: I am going home The message you entered was: I am going home The length of the string entered is: 15 Number of words in message: 4 The message displayed backward is: emoh gniog ma I Run program again? Yes = 1 or No = 0 1 Enter message: hello The message you entered was: hello The length of the string entered is: 5 Number of words in message: 1 The message displayed backward is: olleh Run program again? Yes = 1 or No = 0 C++ Help
Write a C++ program that accepts a string from the user and then displays the string in several different formats using a different function to accomplish each task listed below: a. Reads and displays a string entered by the user. b. Calculates and displays the length of the string. c. Displays the total number of words that appear in the string. d. Display the string backwards. Sample I/O: Enter message: I cannot wait for summer The message you entered was: I cannot wait for summer The length of the string entered is: 24 Number of words in message: 5 The message displayed backward is: remmus rof tiaw tonnac I Run program again? Yes = 1 or No = 0 1 Enter message: I am going home The message you entered was: I am going home The length of the string entered is: 15 Number of words in message: 4 The message displayed backward is: emoh gniog ma I Run program again? Yes = 1 or No = 0 1 Enter message: hello The message you entered was: hello The length of the string entered is: 5 Number of words in message: 1 The message displayed backward is: olleh Run program again? Yes = 1 or No = 0
Write a C++ program that accepts a string from the user and then displays the string in several different formats using a different function to accomplish each task listed below: a. Reads and displays a string entered by the user. b. Calculates and displays the length of the string. c. Displays the total number of words that appear in the string. d. Display the string backwards. Sample I/O: Enter message: I cannot wait for summer The message you entered was: I cannot wait for summer The length of the string entered is: 24 Number of words in message: 5 The message displayed backward is: remmus rof tiaw tonnac I Run program again? Yes = 1 or No = 0 1 Enter message: I am going home The message you entered was: I am going home The length of the string entered is: 15 Number of words in message: 4 The message displayed backward is: emoh gniog ma I Run program again? Yes = 1 or No = 0 1 Enter message: hello The message you entered was: hello The length of the string entered is: 5 Number of words in message: 1 The message displayed backward is: olleh Run program again? Yes = 1 or No = 0 Write a C++ program that accepts a string from the user and then displays the string in several different formats using a different function to accomplish each task listed below: a. Reads and displays a string entered by the user. b. Calculates and displays the length of the string. c. Displays the total number of words that appear in the string. d. Display the string backwards. Sample I/O: Enter message: I cannot wait for summer The message you entered was: I cannot wait for summer The length of the string entered is: 24 Number of words in message: 5 The message displayed backward is: remmus rof tiaw tonnac I Run program again? Yes = 1 or No = 0 1 Enter message: I am going home The message you entered was: I am going home The length of the string entered is: 15 Number of words in message: 4 The message displayed backward is: emoh gniog ma I Run program again? Yes = 1 or No = 0 1 Enter message: hello The message you entered was: hello The length of the string entered is: 5 Number of words in message: 1 The message displayed backward is: olleh Run program again? Yes = 1 or No = 0
Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
char c;
//declaring the function which will perform all the activities
void display();
//calling the function for the first time
display();
cout<<endl<<" Run program again ";
cin>>c;
//if the user says yes the calling the function again
if ((c=='y') or (c=='Y'))
display();
return 0;
}
void display()
{
string str;
int count=0;
int i=1;
//asking the user to input the string
cout<<endl<<"Enter the string";
cin>>str;
//displaying the enterd string
cout<<endl<<"the message you entered is"<<str;
//displaying the length of string using length() method
cout<<endl<<"the length of string entered is"<<str.length();
//calculating the number of words by finding the spaces
for(i=0;i<str.length();i++)
{ if( str[i]==' ')
count++;
}
cout<<"The number of words in the string are "<<count+1;
//displaying the string in reverse
cout<<endl<<"Array in reverse format is ";
for(i=str.length();i>=0;i--)
{
cout<<str[i];
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.