Program: Pig Latin Strings conversts a string into the pig latin form.Rewrite a program from the previous exercise so that it can be used to process a textr 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 for of "HELLO!" is "ello_Hay!" Assume the text might also contain , comma, . period, : colon, ; semicolon.
I have written the following code that will make a pig latin string without any special characters at the end of the word. Here is the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
//Function Check if string begins with a vowel
bool vowel_check(char ch)
{
//use a switch structure to see if its vowel// true equals 1 and false equals zero
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
return true;
default:
return false;
}
}
string rotation(string pig)
{
int length=pig.length();
pig=pig.substr(1,length-1)+pig[0];
return pig;
}
void formationofpig(string pigword)
{
string new_latin;
//Check to see if the first character is vowel or not.
if (vowel_check(pigword[0])==1)
{
//add -way
pigword+="-way";
cout<<"\n\tPig Latin: " <<pigword;
}
else
{
bool falseflag=false;
pigword+="-";
//Get each character from string. We know first one was not voew so extract it
pigword=rotation(pigword);
//go in while loop
for(int counter=1;counter<=pigword.length()-1;counter++)
{
if (vowel_check(pigword[0])==1)
{
cout<<"\n\tPig Latin: " <<pigword+"ay";
falseflag=true;
break;
}
else
{
pigword=rotation(pigword);
}
}
if(!falseflag)
{
int size=pigword.length();
pigword=pigword.substr(1,size)+"-way";
cout<<pigword;
}
}
}
|
Meanwhile, suppose there was a exalamation mark(!). I have made the following alogirithm; however, may the following members guide me or give me an advice if there is a better solution then my method.
Algorithm
1.)Find The length of String(Suppose. HELLO! has a length of 6)
2.) Use
string sentence.find("!")
function to find what index it is.
if(index==sentence.length()-1
then use
sentence.erase(index)
to delete the exclaimation. Also, before that, store that exclaimation into another variable like
string left=sentence[index]
As of now this is what I have, but I want to make sure if im using the correct approach or not :) Thanks guys for reading this!!