C ++检查字符数组中的特定单词

时间:2015-09-18 16:35:41

标签: c++ arrays c-strings

我为我的C ++课做了一个家庭作业,我需要在一个句子中读取该句子的副本然后搜索单词" hell"并用" heck替换它。"教授在编写程序时给了我们一个迷你格式。请注意,我不能在这个程序中只使用字符串字符数组。我教授给我们的样本也向我们展示了我们为每个函数使用的参数,因此我无法更改参数。我现在写的代码如下:

        #include <iostream>
        #include <cctype>
        using namespace std;

        const int MAX_LENGTH = 50; //Assume person will not enter a sentence longer than 47 characters
        const int ZERO = 0;
        void GetSentence(char[]);
        void CopySentence(char[], char[]);
        void MakeUpperCase(char[]);
        void InsertBlankStartEnd(char[]);
        char Sentence[MAX_LENGTH + 1] = "";
        char SentenceCopy[MAX_LENGTH + 1] = "";
        GetSentence(Sentence);
        cout << "Original : " << Sentence << "\n";
        while (Sentence[ZERO] != ZERO)
        {
            //Copy's a sentence while keeping the original (works).
            CopySentence(SentenceCopy, Sentence);
            cout << "Censored : " << SentenceCopy << endl;
            //Converts all elements in SentenceCopy to Uppercase (works)
            MakeUpperCase(SentenceCopy);
            //inserts a blank before the sentence and after the sentence
            InsertBlankStartEnd(SentenceCopy);
            cout << SentenceCopy << endl;
            Sentence[ZERO] = 0;
        }
    return EXIT_SUCCESS;
}
void GetSentence(char Sentence[])
{
    cout << "Enter a sentence ==> " << endl;
    cin.getline(Sentence, MAX_LENGTH + 1);
}
void CopySentence(char Copy[], char Original[])
{
    int i;
    for (i = 0; Original[i]; i++)
    {
        Copy[i] = Original[i];
    }

}
void MakeUpperCase(char Copy[])
{
    int i;
    for (i = 0; Copy[i]; i++)
    {
        Copy[i] = toupper(Copy[i]);
    }
}
void InsertBlankStartEnd(char Copy[])
{
    int i;
    for (i = 0; Copy[i] != NULL; i++)
    {
        Copy[i + 1] = Copy[i];
        Copy[0] = ' ';
    }
}

我知道代码不完整,但我想完成我的InsertBlankStartEnd函数,然后再转到其他代码。该程序工作正常,直到InsertBlankStartEnd函数,我相信这是因为我超出了数组范围范围。当我尝试调试它时,所有程序现在都崩溃了。

0 个答案:

没有答案