首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我会有一个消息分割错误?

为什么我会有一个消息分割错误?
EN

Stack Overflow用户
提问于 2021-12-18 14:45:34
回答 3查看 90关注 0票数 1

使用C,我试图实现一个函数,该函数根据键wordmutated_word转换为string_word。当word"HE"时,用键"QWERTYUIOPASDFGHJKLZXCVBNM",mutated_word应该变成"IT"。但它一直存在分割错误,不确定如何改进。

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

int main(void) {
    string word = "HE" ;
    string string_word = "QWERTYUIOPASDFGHJKLZXCVBNM";
    char mutated_word[strlen(word)]; 
    
    for (int i = 0; word[i] != '\0'; i++) {
        string_word[(int)word[i] - 65] = mutated_word[i];
    }
    
    printf("%s", mutated_word);
}
EN

回答 3

Stack Overflow用户

发布于 2021-12-18 16:11:35

需要终止新字符串的character.

  • Your数组太小,,
  1. ,使用正确的索引类型(int不是正确的)
  2. ,检查字符是否是字母。如果不决定该做什么(在我的示例中,我将所有字母转换为大写字母,其他字符将保留原样)
  3. 不使用魔术数字。使用'A'
  4. Your赋值代替65是错误的,您实际上想要的是相反的东西。
  5. 它将不适用于所有字符编码。

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

int main (void) 
{
    string word = "HE" ;
    string string_word = "QWERTYUIOPASDFGHJKLZXCVBNM" ;
    char mutated_word [strlen(word) + 1]; 
    
    size_t i;
    for (i = 0; word[i] != '\0'; i++)
    {
        if(isalpha((unsigned char)word[i]))
        {
            mutated_word[i] = string_word[toupper((unsigned char)word[i]) - 'A'];
        }
        else
        {
            mutated_word[i] = word[i];
        }
    }
    mutated_word[i] = 0;
   
    printf("%s", mutated_word); 
}

https://godbolt.org/z/4zqq98Y3n

为了使它更便携:

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

ptrdiff_t findIndex(const char ch, const char * restrict dict)
{
    char *result = strchr(dict, ch);
    if(!result) return -1;
    return result - dict;
}

int main (void) 
{
    string word = "He124" ;
    string string_word = "QWERTYUIOPASDFGHJKLZXCVBNM" ;
    string dict = "ABCDEFGHIJKLMNOPQRSTUVXYWZ";
    ptrdiff_t index;
    char mutated_word [strlen(word) + 1]; 
    
    size_t i;
    for (i = 0; word[i] != '\0'; i++)
    {
        if(isalpha((unsigned char)word[i]))
        {
            index = findIndex(toupper((unsigned char)word[i]), dict);
        }
        else index = -1;
        mutated_word[i] = index == -1 ? word[i] : string_word[index];

    }
    mutated_word[i] = 0;
   
    printf("%s", mutated_word); 
}

https://godbolt.org/z/KW8TxxEvq

票数 3
EN

Stack Overflow用户

发布于 2021-12-18 18:42:46

程序崩溃是因为分配顺序不对:string_word[(int)word[i] - 65] = mutated_word[i];试图修改字符串文本,该字符串具有未定义的行为。还请注意,空终止符的目标字符串必须长1字节,必须显式设置该字符串。

下面是一个更可移植的版本:

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

int main(void) {
    const char *word = "HE";
    const char *normal_word = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const char *string_word = "QWERTYUIOPASDFGHJKLZXCVBNM";
    char mutated_word[strlen(word) + 1]; 
    unsigned char c;
    const char *p;
    size_t i;

    for (i = 0; (c = word[i]) != '\0'; i++) {
        if (isupper(c) && (p = strchr(normal_word, c)) != NULL) {
            c = string_word[p - normal_word];
        } else
        if (islower(c) && (p = strchr(normal_word, toupper(c))) != NULL) {
            c = string_word[p - normal_word];
            c = tolower(c);
        }
        mutated_word[i] = c;
    }
    mutated_word[i] = '\0';
   
    printf("%s\n", mutated_word); 
    return 0;
}
票数 1
EN

Stack Overflow用户

发布于 2021-12-18 15:03:35

这是因为您超过了string_word[]的大小,请注意,在每个示例中,'Z'-65 == 25(int)strlen(string_word)

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

https://stackoverflow.com/questions/70404354

复制
相关文章

相似问题

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