Question

In: Computer Science

C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of...

C++

// Program Description: This program accepts three 3-letter words and prints out the reverse of each word

  1. A main(. . . ) function and the use of std::cin and std::cout to read in data and write out data

    as described below.

  2. Variables to hold the data read in using std::cin and a return statement.

#include <iostream >

int main(int argc, char *argv[]) {

.... your code goes here

}//main

Example usage:

>A01.exe
Enter three 3-letter space separated words, then press enter to display the reverse:
cat eye fix
tac eye xif
Enter any key to exit:

Solutions

Expert Solution

Code:

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int reverse_words(string str)
{
   stack <char> stck;
   // Traverse given string and push all characters to stack until we see a space.
     
   for(int i=0;i<str.length();i++)
   {
       if(str[i]!=' ')
       {
           stck.push(str[i]);
       }
       else
       {
           while(stck.empty()==false)
           {
               cout << stck.top();
               stck.pop();
           }
           cout << " ";
       }
   }
   //There is no space after last word
   while(stck.empty()==false)
   {
       cout << stck.top();
       stck.pop();
   }
   return 0;
}
//main
int main()
{
   //Taking string as input
   cout << "Enter three 3-letter space separated words, then press enter to display the reverse:\n";
   std::string str;
   std::getline(std::cin,str);
   reverse_words(str);
   return 0;
}

Screenshot of the program:

Output:


Related Solutions

Use if statements to write a Java program that inputs a single letter and prints out...
Use if statements to write a Java program that inputs a single letter and prints out the corresponding digit on the telephone. The letters and digits on a telephone are grouped this way: 2 = ABC    3 = DEF   4 = GHI    5 = JKL 6 = MNO   7 = PRS   8 = TUV 9 = WXY No digit corresponds to either Q or Z. For these 2 letters your program should print a message indicating that they are not...
Description: You are to develop a Java program that prints out the multiplication or addition table...
Description: You are to develop a Java program that prints out the multiplication or addition table given the users start and end range and type of table. This time several classes will be used. You are free to add more methods as you see fit – but all the methods listed below must be used in your solution. For the class Table the following methods are required: - Protected Constructor – stores the start and size of table, creates the...
only using C (not C++) Implement a program that counts the words and prints their frequencies....
only using C (not C++) Implement a program that counts the words and prints their frequencies. In particular, the program extracts “words” from one or more text files, from a pipe, or from the console, and prints to the console the list of words found and their associated frequencies. Note that your project submission is restricted to only using the following system calls: open(), close(), read(), write(), and lseek() for performing I/O. You are allowed to use other C library...
Description Write a program that prints out your name, the course ID of this class, what...
Description Write a program that prints out your name, the course ID of this class, what programming/computer courses you've taken. Ask the user for two numbers. Show the sum of the two numbers, the difference, the product and the quotient. For the difference subtract the second number from the first, for the quotient, use the first number as the numerator(dividend) and the second as the denominator(divisor). Sample Output: My name is Jianan Liu, I'm in course CS36. I've taken: C...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Write a C++ program that prints out all of the command line arguments passed to the...
Write a C++ program that prints out all of the command line arguments passed to the program. Each command line argument should be separated from the others with a comma and a space. If a command line argument ends in a comma, then another comma should NOT be added
Write a c program that reads a .img file by rows and columns and prints out...
Write a c program that reads a .img file by rows and columns and prints out the arrays. The .img file contains h(the height) and w(the width) of the text size. An example .img file would be: 2 4 DFJSK HJ5JF HFDY5
Write a program to reverse the lines of a file and to reverse the words plus...
Write a program to reverse the lines of a file and to reverse the words plus the letter's of each word within each line using ArrayList. A file name mobydick.txt Example: Original file contains the following MOBY DICK; OR THE WHALE by Herman Melville CHAPTER 1 Loomings. Out put should be .sgnimooL 1 RETPAHC ellivleM namreH yb ELAHW EHT RO ;KCID YBOM its for java eclipse
Write in C++ Write a program that accepts the names of three political parties and the...
Write in C++ Write a program that accepts the names of three political parties and the number of votes each received in the last mayoral election. Display the percentage of the vote each party received.   Be sure to provide labels (party name) and number-align your output values.
Please code in C# - (C - Sharp) Assignment Description Write out a program that will...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input. Tasks The program needs to contain the following A comment header containing...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT