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

标签 c arrays string

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

#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;
}

如果我输入单词是( - 这是我想在文本中更改的单词),然后输入检测到的单词(它比第一个输入的单词有更多的符号 - 这是我想要插入的单词返回的文本“was”)

Console output is: There detected

我不能在这个代码方法或类似的东西中使用,我只能使用 if() 和 for 循环。 谁能帮助我了解如何做到这一点?

最佳答案

首先让我们弄清楚基本规则 - 在本练习中不允许使用字符串函数/方法,您必须主要使用 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() 等函数来测试单词后面可能出现的标点符号会容易得多

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

尚未完成:您需要向此代码添加大量错误检查 - 如果替换使文本大于包含它的数组怎么办?您可以通过测试单词后面的字符来检查单词是否包含在另一个单词中,但无法测试工作之前的字符,例如“其他”与“另一个”。

关于用 C 中的另一个单词更改字符串中的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40331217/

相关文章:

c++ - 基于CLRS的合并排序C++上的算法简介(带反转计数)

ios - 在 iOS 中使用字符串的最佳方式

pandas - 在 Pandas 中解析字符串

无法在 PIC32 中定义大数组?

c - a.exe 已停止运行快速排序程序

java - 数组实例化继承

string - Scala 原始字符串 : Extra tabs at the start of each line

c++ - 更改程序的扩展名

c - int 与 size_t 与 long

c - 如何在 C 中声明可变大小的二维数组?