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

C++ Extra Credit problem1 Due 3 November 2017 CSDP 222 Fall 2017 A children\'s p

ID: 3601123 • Letter: C

Question

C++ Extra Credit problem1 Due 3 November 2017 CSDP 222 Fall 2017 A children's pastime is to convert regular English sentences into Pig Latin. The process is to take the first letter of a word off and place that letter at the end of the word. You then add the 2-letter sequence "AY" to the end of the word. For example: English: ISLEPT MOST OF THE NIGHT. Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY Write a program that reads a couple of sentences and converts the words to pig latin. Input will be one or two sentences. Output will be the original sentence and the corresponding pig latin translation. Output should be to the screen. The input will be provided in ExCr1.txt Have fun!

Explanation / Answer

//Please see the code and output below:

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


string convertToPigLatin(string englishStr){

    int length = 0, index = 0;
    string pigLatinWord, pigLatinSentence = "";

    while (englishStr[index] != ''){
         if (englishStr.find(' ', index) != -1){
            length = englishStr.find(' ', index);
            length -= index;
            pigLatinWord = englishStr.substr(index, length);
            pigLatinWord.insert(length, "AY");
            pigLatinWord.insert(length, 1, englishStr[index]);
            pigLatinWord.erase(0, 1);
            index += length + 1;
        }
        else{
            pigLatinWord = englishStr.substr(index);
            length = pigLatinWord.length();
            pigLatinWord.insert(length, "AY");
            pigLatinWord.insert(length, 1, englishStr[index]);
            pigLatinWord.erase(0, 1);
            index = englishStr.length();
        }

        pigLatinSentence += (pigLatinWord + " ");
    }
    return pigLatinSentence;
}


int main()
{


   string item_name;
   ifstream openFileName;
   openFileName.open("ExCr1.txt");
   string readLine;

   string strPigLatin;
   while(getline(openFileName, readLine))
   {
       cout << "English: " << readLine <<endl<<endl;
       strPigLatin=convertToPigLatin(readLine);
       cout << "Pig Latin: " << strPigLatin <<endl<<endl;
   }

    return 0;
}

OUTPUT:

English: I SLEPT MOST OF THE NIGHT

Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY