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

Write a program using C++ (nothing complicated just the using the basic C++ stuf

ID: 3749517 • Letter: W

Question

Write a program using C++ (nothing complicated just the using the basic C++ stuff)

Computers have long been used to encode and decode messages. The Caesar Shift Cipher from Homework I is one simple scheme that encodes each letler in a message separately. A simple scheme to encode words in a message is Pig Latin. For this assignment, we will translate English words in lowercase to a very simple form of Pig Latin. (Note: This is much simpler than a similar CS 210 assignment that you may or may not have completed.) There are only two ules: 1. If a word start with a vowel, simply append "ay" to the end of the word. Example: insect becomes insectay 2. Otherwise, remove the first letter of the word, append it to the end of the word, and then append "ay" Example: dog becomes ogday. Write a program in a file named piglatin.cpp that will read lines of text from a file. For each word in a line, the program will translate the word into Pig Latin and output it to a file. Assume that the input file meets the following specification: All characters are lowercase alphabetic characters or spaces. Le, there are no uppercase letters, digits, or symbols in the input . The first character in a line is not a space. .The last character of a line (ie, before the newline) is not a space. There is exactly one space between each word in a line. .There are no blank lines. The output file must meet the same specification. That is, the translated words of a line of input must appear in the same line in the output separated by one space and each line must end with a non-whitespace character and a newline. The program must be implemented using (at least) the following functions: A function that receives a C++string that is a line of words in all lowercase letters and passes back a string that is the Pig Latin translation of the word. Eg translate sentence english sentence, piglatin sentence) A function that receives a C++string that is a word in all lowercase letters and passes back a string that is the Pig Latin translation of the word. Eg translate word (english word, piglatin word) The idea is for the main program to read in lines of the file and call the translate sentence function, and for the translate sentence function to find the words in the line and call the translate word function and build up the translated sentence. Reminder: C++strings are defined in the

Explanation / Answer

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

/*

This is translate word function which translates a word into piglatin

*/

void translate_word(std::string source, std::string & dest){

//check if the string is not empty

if(!source.empty()){

//not a vowel to start with

if(source.at(0) != 'a' && source.at(0) != 'e' && source.at(0) != 'i' && source.at(0) != 'o' && source.at(0) != 'u') {

dest.append(source.substr(1));

dest.push_back(source.at(0));

} else {

dest.append(source);

}

   //always append ay

dest.append("ay");

}

}

void translate_sentence(std::string source, std::string & dest)

{

size_t startOfWord = 0;

size_t endOfWord = source.find(' ');

//check if line has spaces

if(endOfWord == string::npos) {

std::cout<<"No spaces in this string"<<endl;

}

std::string word;

std::string convertedWord;

//loop through till we have spaces in the line

while(endOfWord != string::npos){

word = source.substr(startOfWord, endOfWord-startOfWord);

translate_word(word, convertedWord);

dest.append(convertedWord);

startOfWord = endOfWord + 1;

endOfWord = source.find(' ', startOfWord);

dest.append(" ");

word.clear();

convertedWord.clear();

}

//we need to find the last word in the line

word = source.substr(startOfWord, source.length() - 1 - startOfWord);

translate_word(word, convertedWord);

dest.append(convertedWord);

}

/*

This is the start of the main program which takes agruements

*/

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

{

//in case filenames is not passed we will return 0

if(argc < 3) {

return 0;

}

// The first arguement will be the input filename as mentioned in the problem statement

std::string inputFile = argv[1];

// The second arguement will be the output filename as mentioned in the problem statement

std::string outputFile = argv[2];

ifstream inFile;

inFile.open(inputFile);

ofstream outFile;

outFile.open(outputFile);

if (!inFile || !outFile) {

cerr << "Unable to open file";

exit(1); // call system to stop

}

std::string inputLine;

while(getline(inFile, inputLine)) {

std::string outputLine;

translate_sentence(inputLine, outputLine);

outFile << outputLine << endl;

}

//close file handles

inFile.close();

outFile.close();

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote