Pig Latin Translation not working properly

Ok first of I know my code is all over the place which is probably why I'm lost but anyways this program is supposed to have a word separator and pig latin function. The word separator basically separates

"ExceedYourExpectations" to "Exceed your expectations".

I am confident I got this function right but I am having problems translating that result string to pig latin which moves the first letter of the word to the end and add "ay" to it which would display the pig latin string of

"xceedEsay ourysay xpectationsesay"

However the pig latin translation isn't working. Can anyone help me? Would very much appreciate it. Thanks!

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
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>

using namespace std;

// function prototype
void wordSep(string&);
string pigLatinString(string);

int main()
{
	string input;
	string converted;

	//display unformatted sentence
	cout << "Enter An Input String: " << endl;
	cin >> input;

	//seperate the words according to format
	wordSep(input);

	// display new formatted sentence
	cout << "\nResult String: " << input << endl;

	pigLatinString(input);


    converted += pigLatinString(input) + " ";

    cout << "\nPig Latin: " << converted << endl;
    converted = "";

	return 0;
}
// ====================================================
// function definition - converts input to a string where words are separated by spaces and only the
//first word starts with a capital letter
void wordSep(string &input)
{
	char tempLetter; //temporarily stores a letter from &input
	int length;
	length = input.size(); // get original length to use in the loop


	for (int count = 1; count < length; count++) // count starts at 1 to ignore first word(1st capitalization)
		{
			tempLetter = input[count];
			// if uppercase character is found add a space
			if (isupper(tempLetter))
			{
				input.insert(count, 1 ,' ');
				++count; //after insertion of a space character we need to add 1 to the index (this makes count go back to the capital letter)

				//set the letter to lowercase (b/c only first word starts with an uppercase letter)
				input[count] = tolower(input[count]);
			}
		}
}


string pigLatinString(string input)
{

string firstChar = input.substr(0,1);

string restChar = input.substr(1, input.size()-1);

return restChar + firstChar + "ay";

}
Last edited on
Anyone?
comments and links within program, shout if anything's unclear:
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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cctype>
#include <locale>

int main()
{
    std::string input = "ExceedYourExpectations";

    std::vector<std::string> myVec{};
    auto itr_trail = std::find_if(input.begin(), input.end(), isupper);//find first upper-case
    //http://en.cppreference.com/w/cpp/algorithm/find

    while (itr_trail != input.end() )
    {
        auto itr_adv = std::find_if(itr_trail+1, input.end(), isupper);//find next upper-case
        {
            std::string myString {itr_trail, itr_adv};//make string from first and next upper-case
            myVec.push_back(std::move(myString));//move string to vector
            itr_trail = itr_adv;//advance trailing iterator
            if(itr_adv == --input.end())//check leading iterator
            {
                break;
            }
        }
    }
    if(isupper(input.back()))myVec.push_back((std::string{input.back()}));//if last letter is uppercase

    for (auto i = 0; i < myVec.size(); ++i)
    {
        if (i == 0)
        {
            std::cout << myVec[i].substr(1) << myVec[i][0] << "say ";
        }
        else
        {
            std::locale loc{};
            std::cout << myVec[i].substr(1) << tolower(myVec[i][0], loc) << "say ";
            //http://en.cppreference.com/w/cpp/locale/tolower
        }
    }
    std::cout << "\n";
}
//http://www.cplusplus.com/forum/beginner/214171/ 
Thank you for helping, but the code is supposed to have 2 functions and the "Exceed Your Expectations" was just an example. Is it possible if you just tweak my original code? I know I'm doing something wrong or I'm missing something.

This is what I was assigned to do:

1. Ask the user to enter an input string.
2. Call the word separator function with the input string as attachment
3. Display result string
4. Call Pig Latin function with the result string of the word separator function as argument
5. Display the result pig latin string.

However when I run the program, the pig latin isn't working.
Last edited on
you can reverse engineer the solution into the 2 functions required quite easily. lines 10-29 become the word separator function - i'd choose it's return value to be the std::vector<std::string> myVec as in the previous post and then pass this vector to the Pig Latin function (corresponding to lines 31-45 above) which then returns void - play around with the code or try and/or glean some ideas from it and adapt it to your exact requirements
Ok I find out why the pig latin was displaying the same output as the result string so I changed it to

 
cout << "\nPig Latin: " << converted << endl;


but it still isn't displaying the output that I want it to for the pig latin translation.

FOR EXAMPLE: If the input string is

"NotYourBudget"

the Word separator function will be

"Not your budget"

However when I call the pig latin function the output shows

"ot your budgetNay"

instead of "otNay ouryay udgetbay" <--- which would be the correct translation.

Can anyone help me?

Topic archived. No new replies allowed.