用C中的另一个单词改变字符串中的单词

时间:2016-10-30 16:35:06

标签: c arrays string

我正在寻找一种方法来改变字符串中的单词。 我想把单词换成另一个单词,这个单词的符号多于第一个单词。 首先,我计算我想要用另一个改变的单词的符号, 比我计算在我的程序中作为char数组(字符串)的文本中有多少这个单词,现在我想用更多符号的单词更改这个单词。 有代码:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{
    Start:
    printf("This program can change word or symbols combanation in text\n\n"); 
    char text[550] = "There was a grocery shop in a town. Plenty of mice lived in that grocery shop.\nFood was in plenty for them. hey also wasted the bread\nbiscuits and fruits of the shop.\nThe grocer got really worried. So, he thought I should buy a cat\nand let it stay at the grocery. Only then I can save my things.\nHe bought a nice, big fat cat and let him stay there. The cat had a nice\nime hunting the mice and killing them.\nThe mice could not move freely now. They were afraid that\n\n";
    printf("%s\n", text);
    int symbols_in_text = 0;
    for (int f = 0;f < 550;f++)
    {
        if (text[f])
        {
            symbols_in_text++;
        }
        else
        {
            break;
        }
    }
    printf("Text has %i symbols\n\n", symbols_in_text);
    char b[15];
    printf("Which word would you like to chenge in text above?\n(Maximum quantity of Symbols is 15)\nType word or some symbol(s)\n--->>");
    cin >> b;
    int word_symbols=0;
    for (int a = 0;a < 15;a++)
    {
        if (b[a])
        {
            word_symbols++;
        }
        else
        {
            break;
        }
    }
    printf("Word which you have entered  has %i symbols\n", word_symbols);
    int word_in_text = 0;  
    for (int i = 0; i < 550 - word_symbols; i++)
    {
        int found = 1;
        for (int j = 0; j < word_symbols; j++)
        {
            if (b[j] != text[i + j])
            {
                found = 0;
                break;
            }
        }
        if (found && text[i + word_symbols]==' ')
        {
            word_in_text++;
        }
    }
    printf("Founded %i %s as the word an is Not the some part of the other word\n", word_in_text,b);
    if (word_in_text != 0)
    {
        char input_word[15];
        printf("Enter the word which you want to  insert for first entered word\nWarning:You have to enter the world which have %i symbols\n--->>", word_symbols);
        cin >> input_word;
        int in_word_symbols = 0;
        for (int a = 0;a < 15;a++)
        {
            if (input_word[a])
            {
                in_word_symbols++;
            }
            else
            {
                break;
            }
        }
        printf("The word which you have entered has %i symbols\n\n", in_word_symbols);
        if (word_symbols == in_word_symbols)
        {
            for (int i = 0; i < 550 - word_symbols; i++)
            {
                int found = 1;
                for (int j = 0; j < word_symbols; j++)
                {
                    if (b[j] != text[i + j])
                    {
                        found = 0;
                        break;
                    }
                }
                if (found != 0 && text[i + word_symbols] == ' ')
                {
                    for (int j = 0; j < in_word_symbols; j++)
                    {
                        text[i + j] = input_word[j];
                    }

                }
            }
            printf("The result is--->>\n%s\n\n//////////////////////////////////////////END OF////////////////////////////////////////\n\n", text);
        }
        else if (in_word_symbols > word_symbols)
        {
            int text_char_index = 0;
            step1:
            for (int count = 0; count < 550 - word_symbols; count++)
            {
                text_char_index++;
                int found = 1;
                for (int j = 0; j < word_symbols; j++)
                {

                    if (b[j] != text[count + j])
                    {
                        found = 0;
                        break;
                    }

                    if (found != 0 && text[count + word_symbols] == ' ')
                    {   
                        count = text_char_index-1;
                        goto index_changing;
                        insert:
                            for (int c = 0; c < in_word_symbols; c++)
                            {
                                text[count + c] = input_word[c];
                            }
                            if (count > 500)
                            {
                                goto printing_result;
                            }
                            else 
                            {
                                continue;
                            }
                    }
                }
            }
        index_changing:

                for (int l = 466; l > text_char_index + word_symbols; --l)
                {
                    text[l] = text[l + 1];
                }
                goto insert;
            printing_result:
            printf("The result is--->>\n%s\n\n//////////////////////////////////////////END OF////////////////////////////////////////\n\n", text);
        }
    }
    goto Start;

    return 0;
}

如果我输入单词is( - 它是我想在文本中更改的单词)而不是我输入检测到的单词(其中包含的符号多于第一个输入的单词 - 它是我要插入的单词作为回报的文本&#34;是&#34;)

  

控制台输出是:检测到

我无法在此代码方法或类似方法中使用,我只能使用if()和for循环。 谁能帮我理解怎么做?

1 个答案:

答案 0 :(得分:0)

首先让我们直截了当 - 你不允许在本练习中使用字符串函数/方法,你必须使用主要if语句和循环来编写明确的代码。

不是创建一个完整的单独代码块来处理比原始代码更大的替换字,而是将其作为原始替换代码的一部分处理 - 以及处理替换字更小的可能性。两者都需要根据原始单词和替换单词之间的差异以一种方式或另一种方式移动原始文本。

以下是对代码执行上述操作的返工,并进行了大量样式更改和错误修复。它也使它成为一个纯粹的'C'程序:

#include <stdio.h>
#include <stdbool.h>

int main()
{
    printf("This program can change word or character combinations in text.\n\n"); 

    char text[1024] =
        "There was a grocery shop in a town. Plenty of mice lived in that grocery shop.\n"
        "Food was in plenty for them. They also wasted the bread, biscuits and fruits\n"
        "of the shop. The grocer got really worried. So, he thought I should buy a cat\n"
        "and let it stay at the grocery. Only then I can save my things. He bought a\n"
        "nice, big fat cat and let him stay there. The cat had a nice time hunting mice\n"
        "and killing them. The mice could not move freely now. They were afraid that\n";

    while (true) {
        printf("%s\n", text);

        int characters_in_text = 0;

        for (int f = 0; text[f] != '\0'; f++) // not allowed to use strlen()
        {
            characters_in_text++;
        }

        printf("Text has %i characters.\n\n", characters_in_text);

        char word[17];
        printf("Which word would you like to change in text above?\n");
        printf("(Maximum quantity of characters is %d)\n", (int) sizeof(word) - 2);
        printf("Type word or some character(s)\n");
        printf("--->> ");

        (void) fgets(word, sizeof(word), stdin);

        int a, word_characters = 0;

        for (a = 0; word[a] != '\n'; a++)
        {
            word_characters++;
        }

        word[a] = '\0';

        if (word_characters == 0)
        {
            break; // exit program
        }

        printf("Word which you have entered has %i characters.\n", word_characters);

        int words_in_text = 0;

        for (int i = 0; i < characters_in_text - word_characters; i++)
        {
            bool found = true;

            for (int j = 0; j < word_characters; j++)
            {
                if (word[j] != text[i + j])
                {
                    found = false;
                    break;
                }
            }

            if (found)
            {
                char next_letter = text[i + word_characters];

                if (next_letter == ' ' || next_letter == '.' || next_letter == ',' || next_letter == '\n' || next_letter == '\0')
                {
                    words_in_text++;
                }
            }
        }

        printf("Found %i instance(s) of %s that are not part of another word.\n", words_in_text, word);

        char replacement_word[17];
        printf("Enter the word which you want to insert for first entered word.\n");
        printf("(Maximum quantity of characters is %d)\n", (int) sizeof(replacement_word) - 2);
        printf("Type word or some character(s)\n");
        printf("--->> ");

        (void) fgets(replacement_word, sizeof(replacement_word), stdin);

        int replacement_word_characters = 0;

        for (a = 0; replacement_word[a] != '\n'; a++)
        {
            replacement_word_characters++;
        }

        replacement_word[a] = '\0';

        printf("The word which you have entered has %i characters.\n\n", replacement_word_characters);

        int text_shift = replacement_word_characters - word_characters;

        for (int i = 0; i < characters_in_text - word_characters; i++)
        {
            bool found = true;

            for (int j = 0; j < word_characters; j++)
            {
                if (word[j] != text[i + j])
                {
                    found = false;
                    break;
                }
            }

            if (found) 
            {
                char next_letter = text[i + word_characters];

                if (next_letter == ' ' || next_letter == '.' || next_letter == ',' || next_letter == '\n' || next_letter == '\0')
                {
                    if (text_shift > 0)
                    {
                        for (int k = characters_in_text; k > i + word_characters - 1; k--)
                        {
                            text[k + text_shift] = text[k];
                        }

                    }
                    else if (text_shift < 0)
                    {
                        for (int k = i + word_characters; k < characters_in_text + 1; k++)
                        {
                            text[k + text_shift] = text[k];
                        }

                    }

                    characters_in_text += text_shift;

                    for (int j = 0; j < replacement_word_characters; j++)
                    {
                        text[i + j] = replacement_word[j];
                    }
                }
            }
        }
    }

    return 0;
}

你可以看到这个代码在没有库函数的情况下有多复杂 - 使用ctype.h中的ispunct()等函数来测试单词后面可能的标点符号要容易得多

您还在原始代码中计算了许多有用的值,但之后未能在代码中正确使用它们。

还有待处理:你需要进行大量的错误检查才能添加到此代码中 - 如果替换使文本大于包含它的数组,该怎么办?通过测试单词之后的字符但是在工作之前未能测试字符,例如,检查单词是否未包含在另一个单词中,例如, '其他'与'另一个'。