使用Cstrings进行猪拉丁转换

时间:2018-11-15 16:27:28

标签: c++ c-strings

该程序输入用户给出的单词并将其翻译成猪拉丁语。我已经使所有工作几乎完美地工作,但是遇到了两个错误。首先是翻译以辅音开头的单词“ count”时,输出为“ ounttcay”而不是“ ountcay”。第二个错误是,对于三个字母词,例如“ egg”或“ not”,输出为“ egg_ \ 377ay”或“ ottn \ 377ay”。有没有简单的方法来删除重复的字符并摆脱那些数字?

注意-不幸的是,必须使用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;
}

2 个答案:

答案 0 :(得分:0)

您没有终止newArr,并且输入字符串的最后一个索引是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;
}

答案 1 :(得分:0)

您需要在newArr的末尾添加'\ 0',因为strlen不会对其进行计数,因此您无需复制它。 strcat用'ay \ 0'替换'\ 0',但是您没有'\ 0'。

for (int r = 1; r < arrLength; r++) {
     newArr[r-1] = newArr[r];
     newArr[arrLength] = lastChar;
}
newArr[arrLength+1] = '\0';
cout << "Your word in piglatin is " << strcat(newArr, "ay") << endl;