Search code examples
c++c-strings

Pig latin conversion using Cstrings


The program takes in a word given by the user and translates that to pig latin. I've gotten everything to work almost perfectly, but have run into two bugs. The first of which is when translating words that begin with consonants say "count", the output is "ounttcay" instead of "ountcay". The second bug is that when for three letter words like "egg" or "not" the output is "egg_\377ay" or "ottn\377ay". Is there a simple way to remove that duplicate character and get rid of those numbers?

Note - Unfortunately it has to be done using a Cstring

#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

int convertToPigLatin(char arr[50]);
bool isVowel(char ch);

int main() {

    char userInput[50];
    char answer = ' ';

    do {
        cout << "Enter a word to convert it to pig latin" << endl;
        cin.getline(userInput, 50); //get user input

        cout << "Your entered word is " << userInput << endl;

        convertToPigLatin(userInput); //translate user's input into piglatin

        cout << "Would you like to convert another word?" << endl;
        cin >> answer;

        cin.ignore(); //clear past user input
        cin.clear();
    } while (answer == 'Y' || answer == 'y');

    return 0;
}
bool isVowel (char ch) {
    switch (tolower(ch)) { //if the first character of the given input is a vowel
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            return true;
        default:
            return false;
    }
}

int convertToPigLatin(char arr[50]) {
    char newArr[50];

//    string conjunctions[6] = {"and","but","for","nor","yet","the"}; //list of conjunctions not to be converted

    size_t arrLength = strlen(arr); //holds length of input

    for (int i = 0; i < arrLength; i++) { //make sure all characters in input are lower case for easier processing
        newArr[i] = tolower(arr[i]);
    }

    char lastChar = newArr[0]; //save the first character in case it needs to be appended

    if (atoi(arr) || arr[0] == '\0') { //if the input contains a number or begins with a null character print an error
        cout << "Cannot translate inputs that contain numbers" << endl;
        return -1;
    } else if (arrLength <= 2) { // if the input is 2 or less characters
        cout << newArr << endl; //print the input as is
        cout << "Boring! Try somthing more than 2 characters long" << endl;
        return 0;
    } else if ((strstr(newArr, "and") && arrLength == 3) || (arrLength == 3 && strstr(newArr, "but")) || (arrLength == 3 && strstr(newArr, "for")) || (arrLength == 3 && strstr(newArr, "nor")) || (arrLength == 3 && strstr(newArr, "yet")) || (arrLength == 3 && strstr(newArr, "the"))) { //if the input is more than 2 characters long
        cout << newArr << endl; //print the input as is
        cout << "No conjucntions try again!" << endl;
        return 0;
    } else { //if the given input is three characters and is not a conjunction, being translation
        if (isVowel(arr[0])) { //check if input's first character is a vowel
            cout << "Your word in piglatin is "<< strcat(newArr, "ay") << endl; //print that string with 'ay' at the end (i.e. egg'ay')
            return 0;
        } else { //else if the given input starts with a consonant
            for (int r = 1; r < arrLength; r++) {
                newArr[r-1] = newArr[r];
                newArr[arrLength] = lastChar;
            }
            cout << "Your word in piglatin is " << strcat(newArr, "ay") << endl;
            return 0;
        }
    }
    return 0;
}

Solution

  • You're not terminating newArr, and the last index of the input string is arrLength - 1.

    int convertToPigLatin(char arr[50]) {
            // Make sure newArr is properly terminated.
            char newArr[50] = {0};
    
            // [...]
            } else { //else if the given input starts with a consonant
                for (int r = 1; r < arrLength; r++) {
                    newArr[r-1] = newArr[r];
                }
                // Do this outside the loop.
                newArr[arrLength-1] = lastChar;
                // No need for strcat here.
                cout << "Your word in piglatin is " << newArr << "ay" << endl;
            }
        }
        return 0;
    }