The Programming Example: Pig Latin Strings converts a string into the pig Latin
ID: 3539602 • Letter: T
Question
The 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).
source code:
#include
#include
using namespace std;
bool isVowel(char ch);
string rotate(string pStr);
string pigLatinString(string pStr);
int main()
{
string str;
cout << "Enter a string: ";
cin >> str;
cout << endl;
cout << "The pig Latin form of " << str << " is: "
<< pigLatinString(str) << endl;
system("pause");
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;
}
}
string rotate(string pStr)
{
string::size_type len = pStr.length();
string rStr;
rStr = pStr.substr(1, len - 1) + pStr[0];
return rStr;
}
string pigLatinString(string pStr)
{
string::size_type len;
bool foundVowel;
string::size_type counter;
if (isVowel(pStr[0]))
pStr = pStr + "-way";
else
{
pStr = pStr + '-';
pStr = rotate(pStr);
len = pStr.length();
foundVowel = false;
for (counter = 1; counter < len - 1;
counter++)
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else
pStr = rotate(pStr);
if (!foundVowel)
pStr = pStr.substr(1, len) + "-way";
else
pStr = pStr + "ay";
}
return pStr;
}
Remember: For example, the pig Latin form of Hello! is ello-Hay!.
Explanation / Answer
please rate - thanks
any changes needed let me know
sorry pasted the DEV C++ version
fixed it for VC++
#include <iostream>
#include <string>
using namespace std;
bool isPunct(char ch);
bool isVowel(char ch);
string getWord(string,int&);
string rotate(string pStr);
string pigLatinString(string pStr);
int main()
{
string str;
cout << "Enter a string: ";
getline(cin,str);
cout << endl;
cout << "The pig Latin form of " << str << " is: "
<< pigLatinString(str) << endl;
system("pause");
return 0;
}
bool isPunct(char ch)
{
switch (ch)
{
case ',':
case '?':
case '.':
case ';':
case ':':
case '!':
return true;
default:
return false;
}
}
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;
}
}
string rotate(string pStr)
{
string::size_type len = pStr.length();
string rStr;
rStr = pStr.substr(1, len - 1) + pStr[0];
return rStr;
}
string getWord(string s,int& n)
{string out="";
int i;
for(;s[n]!=' '&&s[n]!='';n++)
out=out+s[n];
n++;
return out;
}
string pigLatinString(string str)
{string::size_type len;
bool foundVowel,done=false;
char c=' ';
string sentence="",pStr;
string::size_type counter;
int upto=0;
pStr = getWord(str,upto);
while (!done)
{if(upto>str.length())
done=true;
if(isPunct(pStr[pStr.length()-1]))
{c=pStr[pStr.length()-1];
pStr[pStr.length()-1]='';
}
if (isVowel(pStr[0]))
pStr = pStr + "-way";
else
{
pStr = pStr + '-';
pStr = rotate(pStr);
len = pStr.length();
foundVowel = false;
for (counter = 1; counter < len - 1;
counter++)
if (isVowel(pStr[0]))
{
foundVowel = true;
break;
}
else
pStr = rotate(pStr);
if (!foundVowel)
pStr = pStr.substr(1, len) + "-way";
else
pStr = pStr + "ay";
}
sentence=sentence+" "+pStr+c;
c=' ';
if(!done)
{pStr = getWord(str,upto);
}
}
return sentence;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.