Question

For this assignment, we define three types of "reversal" A "line" reversal reverses the entire line....

For this assignment, we define three types of "reversal"

A "line" reversal reverses the entire line. In a line reversal, "Hello world!" becomes "!dlrow olleH"

A "word" reversal reverses each word. In a word reversal, "Hello world!" becomes "olleH !dlrow"

A "lineword" reversal performs a line reversal and a word reversal. In a lineword reversal, "Hello world!" becomes "world! Hello".

No matter how many spaces between words in the input, the output should have only one space after each word. So a line reversal of "Hello world!" becomes "!dlrow olleH"

Write a C++ program that does line, word and lineword reversals. The program is told which type of reversal to perform by providing a single command line argument, "line", "word", or "lineword".

If no command line arguments are provided, the program should print "MISSING FLAG" and stop.

If an unrecognized command line argument is provided, the program should print "UNKNOWN FLAG" followed by the unrecognized argument, and stop.

If more than one command line argument is provided, the program should print "TOO MANY FLAGS" and stop.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

#include <string>

using namespace std;

void rev(string s) {

for(int i=(s.length()-1); i>=0; i--) {

cout << s[i];

}

}

void lineR(string s) {

string temp = "";

for(int i=0;i<s.length();i++) {

if (s[i]==' ' || i == (s.length()-1)) {

if (i == (s.length()-1))

rev(temp+s[i]+" ");

else rev(temp+s[i]);

temp = "";

} else {

temp += s[i];

}

}

}

void lineRev(string s) {

string sa[100];

int t = 0;

string temp = "";

for(int i=(s.length()-1); i>=0; i--) {

if(s[i]==' ' || i==0) {

rev(temp + s[i]);

temp = " ";

} else {

temp += s[i];

}

}

}

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

string str, type = argv[1];

cout << "Enter string" << endl;

getline(cin, str);

if(argc > 2) {

cout << argv[1];

cout << "TOO MANY FLAGS";

} else if (argc < 2) {

cout << "MISSING FLAG";

} else if (type.compare("word")==0) {

rev(str);

} else if (type.compare("line") == 0) {

lineR(str);

} else if (type.compare("lineword") == 0) {

lineRev(str);

} else {

cout << "UNKNOWN FLAG";

}

}

output:

Add a comment
Know the answer?
Add Answer to:
For this assignment, we define three types of "reversal" A "line" reversal reverses the entire line....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • After reading pages 330 - 336, write a program that takes two command line arguments which...

    After reading pages 330 - 336, write a program that takes two command line arguments which are file names. The program should read the first file line by line and write each line, in reverse order, into the second file. The program should include a "usage" method that displays the correct command line syntax if two file names are not provided. Example: if my input file says Hello, World! then my output file will contain !dlroW ,olleH Hints: Use CaesarCipher...

  • Write a program that uses a recursive function to determine whether a string is a character-unit...

    Write a program that uses a recursive function to determine whether a string is a character-unit palindrome. Moreover, the options for doing case sensitive comparisons and not ignoring spaces are indicated with flags. For example "A nut for a jar of tuna" is a palindrome if spaces are ignored and not otherwise. "Step on no pets" is a palindrome whether spaces are ignored or not, but is not a palindrome if it is case sensitive. Background Palindromes are character sequences...

  • Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text...

    Objective: Use input/output files, strings, and command line arguments. Write a program that processes a text file by removing all blank lines (including lines that only contain white spaces), all spaces/tabs before the beginning of the line, and all spaces/tabs at the end of the line. The file must be saved under a different name with all the lines numbered and a single blank line added at the end of the file. For example, if the input file is given...

  • In C please Create a program that takes two integers as command line arguments (num, length)...

    In C please Create a program that takes two integers as command line arguments (num, length) and outputs a list of the first length multiples of num. num should be included in the returned array. For example: ./a.out 7 5 -> [7, 14, 21, 28, 35] ./a.out 12, 10 -> [12, 24, 36, 48, 60, 72, 84, 96, 108, 120] ./a.out 17, 6 -> [17, 34, 51, 68, 85, 102] Ma Word starts with a vowel add "yay" to the...

  • Background: For this assignment, you will write a small encryption utility that implements a simple encryption...

    Background: For this assignment, you will write a small encryption utility that implements a simple encryption algorithm described below. The program will take one command line argument as an input; this will represent the word which is to be encrypted. As an output, your program will print the encrypted version of the word to the console using a simple printf() statement. This is the only output your program needs to produce. There is an important catch, however: your program is...

  • If i could get any guidance on how to get this started it will be great....

    If i could get any guidance on how to get this started it will be great. My prof. literally just gave us the information below (which are literally just instructions) and I am unsure on how to get started. For this assignment we are going to create a function that reads an input stream and classifies it into “tokens” from a language that we define here. In this language we make the following rules: ● An identifier is a letter...

  • This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and chmod(),...

    This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and chmod(), perror(), and command line arguments. Program: Write a C++ program that will allow you to add messages to a file that has NO permissions for any user. A Unix system has many files that have sensitive information in them. Permissions help keep these files secure. Some files can be publicly read, but can not be altered by a regular user (ex.: /etc/passwd). Other files...

  • This is in C. For this assignment we will write a simple database server. We will...

    This is in C. For this assignment we will write a simple database server. We will be creating a simple database of student records, so let’s describe these first. The format of a student record is as follows: typedef struct student {     char lname[ 10 ], initial, fname[ 10 ];     unsigned long SID;     float GPA; } SREC; Part One – the Server We will create a database server. The job of the server is to accept a...

  • Purpose This assignment should give you experience in using file descriptors, open(), close(), wr...

    Purpose This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and chmod(), perror(), and command line arguments. Program Write a C++ program that will allow you to add messages to a file that has NO permissions for any user. A Unix system has many files that have sensitive information in them. Permissions help keep these files secure. Some files can be publicly read, but can not be altered by a regular user (ex.: /etc/passwd). Other...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT