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

C++: Programming Example: Pig Latin Strings converts a string into the pig Latin

ID: 641005 • Letter: C

Question

C++: Programming Example: Pig Latin Strings converts a string into the pig Latin form, but it processes only one word. Rewrite the program so that it can be used to process a text of an unspecified length. If a word ends with a punctuation mark, in the pig Latin form, put the punctuation at the end of the string. For example, the pig Latin form of Hello! is ello-Hay!. Assume that the text contains the following punctuation marks: , (comma), . (period), ? (question mark), ; (semicolon), and : (colon). (Your program may store the output in a file.)

Explanation / Answer

  
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

bool isVowel(char ch);
string rotate(string pStr, int len);
string pigLatinString(string pStr);

void getNextWord(ifstream& inF, char& ch, string& word);

int main()
{
string str;

char ch;

ifstream infile;
ofstream outfile;

infile.open("Ch7_Ex3Data.txt");
if (!infile)
{
cout << "Cannot open input file. Program terminates." << endl;
return 1;
}

outfile.open("Ch7_Ex3Out.txt");

infile.get(ch);

while (infile)
{
while (ch != ' ')
{
if (ch == ' ')
{
outfile << ch;
infile.get(ch);
}
else
{
getNextWord(infile, ch, str);
outfile << pigLatinString(str);
}
}

outfile << endl;
infile.get(ch);
}

infile.close();
outfile.close();

return 0;
}

bool isVowel(char ch)
{
switch (ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
default:
return false;
}
}

bool isPunctuation(char ch)
{
switch (ch)
{
case ',':
case '.':
case '?':
case ';':
case ':':
return true;
default:
return false;
   }
}

string rotate(string pStr, string::size_type len)
{
string rStr;

rStr = pStr.substr(1, len) + pStr[0];

return rStr;
}

string pigLatinString(string pStr)
{
string::size_type len = pStr.length();

bool foundVowel = false;

bool isPunct = isPunctuation(pStr[len - 1]);
char puncMark;

string::size_type counter;

if (isPunct)
{
puncMark = pStr[len - 1];
len = len - 1;
}

if (isVowel(pStr[0]))                      
pStr = pStr.substr(0,len) + "-way";  
else                                      
{
pStr = pStr.substr(0,len) + '-';

pStr = rotate(pStr, len);                 

len = pStr.length();                  
foundVowel = false;                      

for (counter = 1; counter < len - 1; counter++)
if (isVowel(pStr[0]))
{
foundVowel = true;
   break;
}
else                              
pStr = rotate(pStr, len);

if (!foundVowel)                          
pStr = pStr.substr(1,len) + "-way";
else
pStr = pStr + "ay";
   }

if (isPunct)
pStr = pStr + puncMark;

return pStr;
}

void getNextWord(ifstream& inF, char& ch, string& word)
{
word = ch;

while (ch != ' ' && ch != ' ')
{
inF.get(ch);

if (ch != ' ' && ch != ' ')
word = word + ch;
}
}

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