Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

String exercises Your tasks are to do the following 3 programs: • (1)Write a pro

ID: 3814820 • Letter: S

Question

String exercises

Your tasks are to do the following 3 programs:

• (1)Write a program that will receive a line of text and reverse the order of the words so that they are printed out in LIFO order. For example: Enter a string: the quick brown fox jumped over the lazy dog (dog lazy the over jumped fox brown quick the) There are two simple ways to do this. One way is to use an array of strings and assign each word to an array position. You can then iterate backwards through the array and print out the words. The other way to do this is to push words onto a stack and pop them off after. Since this lab is about using templated classes, you should implement your revString program with a stack.

• (2)Start with the given revString.cpp source file (revString.cpp code at bottom), copy it into a Lab11rev.cpp file -- it uses string streams (an istrstream object) to allow the user to enter a line and then read all the words from the line.

• (3)Write a program that converts a word into Pig Latin. Name it Lab11Piglatin.cpp. For those of you who don't know, Pig Latin is a childhood code language that follows two simple rules:

The Pig Latin translations of words that begin with a consonant are formed by moving the initial consonant to the end and appending "ay"

The translations of words that begin with a vowel are formed by appending "yay"

• Some examples of Pig Latin translations are:

"hello world" becomes "ellohay orldway"

"object oriented" becomes "objectyay orientedyay"

If you wish you can try to extend your program to translate more than one word at a time. "Impressyay ouryay riendsfay."

// file: revString.cpp
// String manipulation example
// This is an inlab exercise and is incomplete

#include <iostream>
#include <strstream> // for string streams
#include <string>
using namespace std;


// MAX_INPUT is the maximum number of characters that we allow to be
// read into a string object with istream::getline()
static const int MAX_INPUT = 256;


int main()
{
char buf[MAX_INPUT];
string line = "";
string word;

cout << "enter a line: ";

/* The following would be the best way to read in a line into a string
* object; however, there is a bug in the code provided by MSVC that
* reads an extra character (so the user would have to hit return twice.
* - see http://support.microsoft.com/support/kb/articles/Q240/0/15.ASP

getline( cin, line );

*/

// The following is a workaround, use istream::getline() instead of
// the function getline.
cin.getline( buf, MAX_INPUT );
line = buf;
  
// open a stream bound to the line
istrstream input( line.c_str() );

while ( input >> word )
{
cout << word << endl;
}

return 0;
}

Explanation / Answer

Ans-(1)

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
//create input and link that input in the text file input.txt...
ifstream inputFile;
inputFile.open("input.txt");
//output files
ofstream outputFile;
outputFile.open("output.txt");

//error message if file open failed
if (inputFile.fail())
cout << "Error opening the file. ";

//constant for max size
const int MAXSIZE = 1024;
//string array and temporary-use string
string words[MAXSIZE];
string str;
//read words from input file
for (int i = 0; (inputFile >> str) && (i < MAXSIZE); ++i)
{
words[i] = str;
}
inputFile.close();

//for showing if read correctly
cout << endl;
for (int i = 0; i < MAXSIZE; ++i)
cout << words[i] << endl

system("pause");
return 0;
}

Ans -(3)   

#include <iostream>
#include <string>
using namespace std;

// takes a string agrument from user and returns the pigLatin word
string pigLatin(string);

int main()
{
string Sentence;

getline(cin, Sentence);
Sentence = pigLatin(Sentence);
cout << Sentence << endl;

return 0;
}

string pigLatin(string word){

//pigLatWord holds word translated in pig latin.
//pigLatSentence holds entire translated sentence.
string pigLatWord, pigLatSentence = "";
int length = 0, index = 0;

while (word[index] != ''){
// .find returns -1 if no match is found
if (word.find(' ', index) != -1){
length = word.find(' ', index);
length -= index; //length - index = the number of characters in a word
pigLatWord = word.substr(index, length);
pigLatWord.insert(length, "ay");

index += length + 1;
}
else{
pigLatWord = word.substr(index);
length = pigLatWord.length();
pigLatWord.insert(length, "ay");
index = word.length();
}

pigLatSentence += (pigLatWord + " ");
}
return pigLatSentence;
}