Pig Latin ALgortithm help!

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!!
You are making this way too complicated. The string class has this other method which you have ignored called std::string::size. So to check if the string has an exclamation at the end, you simply do:

somestring[somestring.size() - 1]
You are making this way too complicated. The string class has this other method which you have ignored called std::string::size. So to check if the string has an exclamation at the end, you simply do:

somestring[somestring.size() - 1]


or you could us the string back method
http://www.cplusplus.com/reference/string/string/back/


2.) Use string sentence.find("!") function to find what index it is.

I think find_first_of would be a better choice
http://www.cplusplus.com/reference/string/string/find_first_of/

I just threw this together quickly
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
#include <iostream>
#include <string>
using namespace std;

string convertToPiglatin(string inputString);

int main()
{
	string english = "Yellow!";

	cout << english << " converted to pig latin is " << convertToPiglatin(english) << endl;

	cin.ignore();
	return 0;
}

string convertToPiglatin(string inputString)
{
	string ay = "ay";
	string vowel = "aeiouAEIOUyY";
	size_t vowelIndex;
	string stringTemp = "";
	string firstLetter = "";

	firstLetter = inputString.front();

	//checks to see if the first letter is a vowel other than Y
	if (vowel.substr(0, 9).find(firstLetter) != std::string::npos)
	{
		ay = "way";
	}
	else
	{	
		vowelIndex = inputString.find_first_of(vowel);
		
		if (firstLetter == "y" || firstLetter == "Y")
			vowelIndex = inputString.substr(1, inputString.size() - 1).find_first_of(vowel) + 1;
		
		stringTemp = inputString.substr(0, vowelIndex);
		inputString = inputString.substr(vowelIndex, inputString.size() - 1);
	}

	//adds !?:; to the back of ay if needed
	if (inputString.find_first_of("!?:;") != std::string::npos)
	{
		ay += inputString.back();
		inputString.pop_back();
	}

	return string(inputString + "_" + stringTemp + ay);
}
Last edited on
Quick question,how will finding the size of the string and subtracting it by one help me determine if there is a exclamation?

Also, Hanson, on line 28 do you mind explaining what's happening on that line of code. Haven't seen that before and those two steing functions are throwing me off.
fahmankhan75 wrote:
Quick question,how will finding the size of the string and subtracting it by one help me determine if there is a exclamation?


This is a string
-------------------------
| A | B | C | D | E | ! |
--^--^--^---^--^--^--
| 0 | 1 | 2 | 3 | 4 | 5|


Length of the above string is 6
Index [6 -1] => [5] => '!'


EDIT:
Alternatively you can use string::back as @Yanson suggested, which I think it even more subtle than what I have suggested and definitely less complicated than what you have
Last edited on
Yanson, on line 28 do you mind explaining what's happening on that line of code

I will try

So I'm not an expert on pig latin, but from what I understand y can either be a consonant or a vowel. For example, when dealing with the word Yellow the y is considered a consonant so Yellow should be ellow_Yay. On the other hand, in the word rhythm, y acts as a vowel so rhythm Is ythm_rhay. Another thing to consider, is if the word begins with a vowel, way is added to the end of the word, and no other changes are needed.

that being said, the following code tries to determin if the first letter of the string is a vowel, and if it is, it changes the string ay = "ay" to ay = "way". The first part vowel.substr(0,9)
this statement cuts off the upper and lower case y in the vowel string, because at the beginning of a word y is not considered a vowel. This returns a substring that looks like this "aeiouAEIOU". I then call the find method on this substring, so it would look like this "aeiouAEIOU".find(firstLetter). I am searching the substring for firstLetter, whatever that happens to be. If fristLetter is not found within the substring then std::string::npos is returned by the find method. So != std::string::npos is saying if firstLetter is found in substring execute the following code.
1
2
3
4
if (vowel.substr(0, 9).find(firstLetter) != std::string::npos)
{
	ay = "way";
}
Last edited on
Smack, makes more sense now! Thank you for your help :)

Yanson, I got the ideas of how to do a pig latin code; however, just looking for a tad more knowledge. I fully understand what your line 28 is doing now :D however that string::npos is really bugging me! I have noticed that in my C++ book too and other programmers here on this forum. There are two string data type. one is size_type(which is all positive integers and the other one is npos(which I don't understand at all or even what it is) please don't refer me to the c++ reference page. Their wordings are a little too advanced for me right now, my opinion. YOU did explain this quite well and I would like you to answer this as well too please.

1.) I don't understand what the npos is doing in that if statement? This is what I'm viewing your line 28 to be now. Please correct me. If (first letter) or vowel is not found then make ay="way";I see a "!" should it not only be equal then?..meaning if the first letter is vowel then set ay="way";

2.) why are you using string::npos? is there not another way to write this "if first letter is vowel and found, then set "ay="way"".

Last edited on
Nevermind Yanson :D did some research and seemed to figure it out now :D. I have a feeling I would have another question though :/ still studying your code :)

THan you though :)
Topic archived. No new replies allowed.