【问题标题】:Pig latin conversion using Cstrings使用 Cstrings 进行猪拉丁语转换
【发布时间】:2018-11-15 16:27:27
【问题描述】:

程序接收用户给出的单词并将其翻译成猪拉丁语。我已经让一切工作几乎完美,但遇到了两个错误。第一个是当翻译以辅音开头的单词时说“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;
}

【问题讨论】:

  • 我不知道有一个c-strings 标签... C++ 有一个std::string 类型/类模板/容器。改用那个。
  • C++ 的第一课是尽快停止使用 C 风格的字符串。这些是异常痛苦和痛苦的根源。你有“神奇的数字”50 在你的代码中大量散布。这真的很成问题,就好像你有一天更新到 100 你可能会错过一个地方并造成一个严重的缓冲区溢出错误。
  • @tadman 应该预料到这一点,我已经编辑了我的解释。不幸的是,使用 Cstrings 是一项功能要求。
  • 哦,这是那些课程之一。在这种情况下,祝你好运,我希望你能度过难关,而不会造成任何持久的伤害或需要长期治疗。
  • “但是为什么是 Santy Claus,你为什么在 C++ 中使用 C 风格的字符串?为什么”。但是你知道吗,那个老格林奇是那么聪明,那么圆滑,他想出了一个谎言,而且他很快就想出来了。 “为什么我可爱的小宝贝……”

标签: c++ c-strings


【解决方案1】:

您没有终止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;
}

【讨论】:

    【解决方案2】:

    您需要在 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多