首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字符串中的单词替换为2D数组中给定的单词

将字符串中的单词替换为2D数组中给定的单词
EN

Stack Overflow用户
提问于 2022-12-03 18:55:01
回答 1查看 32关注 0票数 -1

我目前正在做一个程序,为了更有礼貌地纠正句子中给定的单词。

我正在构建一个函数,这个函数给出了原来的句子和一个2D数组,它存储我们应该寻找的单词,以及我们将用它们替换的单词。

这是我声明“字典”的主要功能:

代码语言:javascript
复制
int main(){
    const char * d1 [][2] =
    {
        { "hey", "hello" },
        { "bro", "sir" },
        { NULL, NULL }
    };
    printf("%s\n", newSpeak("well hey bro", d1) );

}

这个函数的任务是遍历原始字符串的每个指针,并用每个单词的第一个字符来检查它,这可能是“坏”。如果它抓住了第一个字母,那么它将遍历单词的其余部分,如果它一直走到单词的末尾,它将跳过原来的单词并替换为'good‘字。

这就是功能本身:

代码语言:javascript
复制
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>

char * newSpeak ( const char * text, const char * (*replace)[2] )
{   
    char * result = (char*)malloc( sizeof(char) );
    int resIndex = 0; // Pointer to final text
    int matches = 0; // 1 - Matches word from library, 0 - Does not
    // Run through the whole original text
    for ( int index = 0; text[index] != '\0'; index++ ){

        for ( int line = 0; replace[line][0] != NULL; line++ ){
            // If the first letter of the word matches, do the others match too?
            // If yes, don't rewrite the original word, skip it, and write the replacement one by one.
            if ( replace[line][0][0] == text[index] ){
                matches = 1;
                // Check one by one if letters from the word align with letters in the original string
                for ( int letter = 0; replace[line][0][letter] != '\0'; letter++ ){
                    if ( replace[line][0][letter] != text[index + letter] ){
                        matches = 0;
                        break;
                    }
                }
                // If the whole word matches, skip what would be copied from original text (the bad word) and put in replacement letter by letter
                if ( matches == 1 ){
                    // Push pointer of original string after the word
                    index += strlen( replace[line][0] );
                    for ( int r = 0; r < strlen( replace[line][1] ); r++){
                        result = (char*)realloc(result, (strlen( result ) + 1) * sizeof(char));
                        result[resIndex + r] = replace[line][1][r];
                        index += r; 
                    }
                }
            }
        }
        if ( matches == 0 ){
            result = (char*)realloc(result, (strlen( result ) + 1) * sizeof(char)); 
            result[resIndex] = text[index];
        }
        resIndex++;
    }
    return result;
}

运行后,我的预期结果是well hello sir,但是函数只返回well hello

我正在寻找一个解释,为什么循环将停止,而不是检查其余的字符串,任何帮助将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2022-12-03 19:22:55

至少这个问题:

strlen( result )result = (char*)realloc(result, (strlen( result ) + 1) * sizeof(char));中无效,因为result没有指向字符串。缺空字符。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74669710

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档