Pig Latin Strings

Hi coders. hope you're having a safe thanksgiving everyone. I got another good one. Here is my code so far (down below). It's having an issue with "searching your code for a specific pattern:" which is telling me it's supposed to look like this "e-thay Internet-way. You-way an-cay atch-way". Here are the instructions:

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, colon) store the output in Ch7_Ex3Out.txt.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

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

using namespace std;

string translateLine(string);
string translateWord(string);
string getNextWord(string, int);

bool isVowel(char);
bool isPuncutation(char);

int main()
{
    ifstream inFile;
    ofstream outFile;
    string sentence;
    string pigSentence;
    string menuChoice;

    inFile.open("CH7_Ex3Data.txt");
    outFile.open("CH7_Ex3Out.txt");
    outFile << fixed << showpoint << setprecision(2);

    cout << "Your sentence: ";
    cout << endl;
    getline(cin, sentence);

    pigSentence = translateLine(sentence);
    cout << pigSentence << endl;
    return 0;
}

string translateLine(string sentence)
{
    int i = 0, length;
    length = sentence.length();
    string word, pigWord, pigSentence="";
    while (i < length)
    {
        word = getNextWord(sentence, i);
        i = i + word.length() + 1;
        pigWord = translateWord(word);
        pigSentence = pigSentence + pigWord + " ";
    }
    return pigSentence;
}

string getNextWord(string sentence, int i)
{
    string word;
    char letter;
    letter = sentence[i];
    while (letter != ' ' && i < sentence.length())
    {
        word = word + letter;
        i = i + 1;
        if (i < sentence.length())
            letter = sentence[i];
    }
    return word;
}

string translateWord(string word)
{
    int i = 0;
    string pigWord;
    string first = "";
    string second = "";
    if (isVowel(word[0]) == true)
    {
        pigWord = word + "way";
    }
    else
    {
        if (isVowel(word[i]) == false)
        {
            while (i < word.length() && word[i] != 'a' && word[i] != 'e' && word[i] != 'i' && word[i] != 'o' && word[i] != 'u')
            {
                first = first + word[i];
                i++;
            }
            while (i < word.length())
            {
                second = second + word[i];
                i++;
            }
        }
        pigWord = second + first + "ay";
    }
    return pigWord;
}

bool isVowel(char letter)
{
    bool vowel;
    letter = tolower(letter);
    if (letter == 'a' || letter == 'e' ||
        letter == 'i' || letter == 'o' ||
        letter == 'u' )
    {
        vowel = true;
    }
    else
    {
        vowel = false;
    }
    return vowel;
}
Hello Frank5093,

Some things I noticed when I tried to compile the program.

You define variables an "int"s, but an example is:
1
2
3
4
5
6
7
8
string getNextWord(string sentence, int i)
{
    string word;
    char letter;

    letter = sentence[i];

    while (letter != ' ' && i < sentence.length())

When you get to the while loop sentence.length() the ".length()" and ".size()" functions return a "size_t" type of variable. The only guarantee is that "size_t" is an "unsigned" type. The value returned could be an int or a long. This would depend on your computer and header files. Also called "implementation defined".

The compiler should produce a warning something like:

signed/unsigned mismatch


Most of the time this will not be any real problem and the program will work, but it is a potential problem. It is best to learn when to use "size_t".

Some other little things:
i = i + 1; can be shortened to i++; and word = word + letter; and the like can be shortened to
word += letter;.

The function "isVowel" can be shortened to:
1
2
3
4
5
6
7
8
9
10
11
12
13
bool isVowel(char letter)
{
    letter = tolower(letter);

    if (letter == 'a' || letter == 'e' ||
        letter == 'i' || letter == 'o' ||
        letter == 'u')
    {
        return true;
    }

    return false;
}

You do not need all the extra code if you can use what you have and the way the language works. since the if statement returns true everything else is skipped and if the if statement is false the only thing left is to return false.

And for something like: if (isVowel(word[0]) == true). First understand an if statement, while loop and the middle part of a for loop will take whatever is in there and evaluate it to either a (0) zero or something other than (0) zero. This is then converted to a bool type of (0)zero for false or (1) for true. Saying (== true) or (false) is kind of redundant since it will eventuall become either (0)zero false or (1) true.

You can use this to your advantage since the function returns a bool in the first place by writing
if (isVowel(word[0])) or (!isVowel(word[0])) for needing a false to be true. It saves a little time and typing.

The last thing I noticed is the test string "This is a test." produced isThay isway away est.tay Notice that the period did not come out where it should.

For fun you might want to work on changing the whole string to lower case then capitalize the first letter of the first transformed word.

Andy
Last edited on
1
2
3
4
5
6
bool isVowel(char letter)
{
	static const char vowels[] {"aeiouAEIOU"};

	return std::strchr(vowels, letter) != NULL;
}

With Andy's suggestions and others and dealing with punctuation, consider:

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

string translateLine(const string&);
string translateWord(const string&);
string getNextWord(const string&, size_t);

bool isVowel(char);
bool isPunctuation(char);

int main()
{
	/*
	ifstream inFile("CH7_Ex3Data.txt");
	ofstream outFile("CH7_Ex3Out.txt");

	if (!inFile.is_open() || !outFile.is_open())
		return (cout << "Cannot open files\n"), 1;

	outFile << fixed << showpoint << setprecision(2);
	*/

	string sentence;

	cout << "Your sentence:\n";
	getline(cin, sentence);

	cout << translateLine(sentence) << '\n';
	return 0;
}

string translateLine(const string& sentence)
{
	string pigSentence;

	for (size_t i = 0; i < sentence.length(); ) {
		const string word {getNextWord(sentence, i)};

		pigSentence = pigSentence + translateWord(word) + " ";
		i += word.length() + 1;
	}

	return pigSentence;
}

string getNextWord(const string& sentence, size_t i)	// Only used by translateLine
{
	string word;

	for (char letter {}; i < sentence.length() && (letter = sentence[i]) != ' '; ++i)
		word += letter;

	return word;
}

string translateWord(const string& word)
{
	string pigWord;

	if (isVowel(word[0]))
		pigWord = word + "way";
	else {
		string first, second;
		size_t i {};

		if (!isVowel(word[i])) {
			while (i < word.length() && !isVowel(word[i]))
				first += + word[i++];

			while (i < word.length())
				second += word[i++];
		}

		if (char last = second.back(); isPunctuation(last))
			pigWord = second.substr(0, second.size() - 1) + "-" + first + "ay" + last;
		else
			pigWord = second + "-" + first + "ay";
	}

	return pigWord;
}


bool isVowel(char letter)
{
	static const char vowels[] {"aeiouAEIOU"};

	return std::strchr(vowels, letter) != NULL;
}

bool isPunctuation(char letter)
{
	static const char punc[] {",.?;:!"};

	return std::strchr(punc, letter) != NULL;
}



Your sentence:
Hello! hello?
ello-Hay! ello-hay?

Thank you everyone, everything is working great!
Topic archived. No new replies allowed.