简体   繁体   中英

Pig Latin Program Not Working

Ok basically this program is supposed to have two tasks.

Task 1 : is a word separator function which takes a string like this

FOR EXAMPLE: "ExceedYourExpectations" to "Exceed your expectations"

Task 2 : Pig Latin function that moves the first letter of the word to the end and add "ay"

FOR EXAMPLE: "Exceed your expectations" to "xceedEay ouryay xpectationseay"

and in the main function of the program it will do this

  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.

But for some reason the pig latin function isn't working properly. Instead it puts the first letter of sentence and put it in the end resulting in the output of

"xceed your expectationsEay" instead of "xceedEay ouryay xpectationseay"

Can someone help me pls?? I been at this for about 2 days and I still can't get it right. THANKS IN ADVANCE!

#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";

}

The reason your pig latin function is not giving you the output you are expecting is because you are passing in the entire string (with spaces and all) as input. If you pass in your input word for word instead, you will get the result you are expecting, as shown below:

#include <iostream>
using namespace std;

// This is your function
string pigLatinString(const string& input) {
    string firstChar = input.substr(0,1);

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

    return restChar + firstChar + "ay";
}

// This function takes your input string and calls 
// your pig latin function once for each word
string callPigLatinStringWordForWord(const string& input) {
    string result = "";

    // Find first word
    size_t startPosition = 0;
    size_t spacePosition = input.find(' ', startPosition);

    while (spacePosition != string::npos) {
        string word = input.substr(startPosition, spacePosition - startPosition);
        result += pigLatinString(word) + " ";

        // Find next word
        startPosition = spacePosition + 1;
        spacePosition = input.find(' ', startPosition);
    }

    // Finally transform the final word
    result += pigLatinString(input.substr(startPosition));

    return result;
}

int main() {
    string input = "Exceed your expectations";
    string result = callPigLatinStringWordForWord(input);
    cout << result;

    return 0;
}

When transforming strings where the output will be a different length than the input, it's often easier and more efficient to create a new string while doing a single forward pass over the original string.

std::string pigLatinString(const std::string &source) {
    std::string target;
    char first = '\0';
    for (const auto ch : source) {
        if (first == '\0') {
            if (std::isspace(ch)) {
                target += ch;
            } else {
                // remember the first char until the end of the word
                first = ch;
            }
        } else {
            if (std::isspace(ch)) {
                 target += first;
                 target += "ay";
                 first = '\0';
            }
            target += ch;
        }
    }
    if (first != '\0') {
        target += first;
        target += "ay";
    }
    return target;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM