You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言文本编解码程序解码功能运行异常问题排查求助

问题说明

编写了基于双数组映射的文本编解码程序:一个数组存储大小写英文字母,另一个存储对应编号数字。编码时将输入字符串拆分为单个字符,匹配字符在字母数组的索引,替换为数字数组同索引位置的数值;解码采用反向映射逻辑。当前编码功能可正常运行,解码功能失效,原代码如下:

#include<stdio.h>
#include<string.h>

int encode(char dec[], int enc[]);
int decode(int enc[], char dec[]);
int indx(char arr[], char x);

int main()
{
    int c;
    char dec[] = {'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z'};
    int enc[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52}; 
    
    while(1)
    {
        printf("1.Encode\n");
        printf("2.Decode\n");
        printf("3.Exit\n");
        printf("Enter your choice");
        scanf("%d", &c); 
        if(c==1)
        { 
            encode(dec, enc);
        }
        else if(c==2)
        {
            decode(enc, dec);
        }
        else if(c==3)
        {
            break;
        }
        else{
            printf("Invalid choice!");
        }
        
        
    }
    
    return 0;  
}
int indch(char arr[], char x)
{
int arrlen = 52;
int indx;

for(int i=0; i<arrlen; i++)
{
        if(arr[i] == x)
        {
                indx = i;
                break;
        } 
}
return indx;
}

int indin(int arr[], int x)
{
int arrlen = 52;
int indx;

for(int i=0; i<arrlen; i++)
{
        if(arr[i] == x)
        {
                indx = i;
                break;
        } 
}
return indx;
}

int encode(char dec[], int enc[])
{
    char txt[100];
    int arrlen;
    int ind;
    char a;
    int i = 0;
    printf("Enter the text to be encoded:");
    scanf("%s", txt); 
    arrlen = sizeof txt / sizeof txt[0]; 
    while(txt[i]!= '\0')
    {
        a = txt[i];
        ind = indch(dec , a);
        printf("%d ",enc[ind]);
        i++;
    }
    return 0;
}

int decode(int enc[], char dec[])
{
    int txt[100];
    int arrlen=0;
    int ind;
    int a;
    printf("Enter the code to be decoded:");
    for(int j=0;j<100;j++)
    {
        scanf("%d", &txt[j]);
        if(txt[j]=='\n')
        {
            break;
        }
        arrlen++; 
    }
    

    for(int i=0; i<arrlen; i++)
    {
        a = txt[i];
        ind = indin(enc, a);
        printf("%d", dec[ind]);
    }
    return 0;
}
错误点定位
  • 函数前置声明与实现不匹配:代码开头声明了int indx(char arr[], char x);,但实际实现的是字符查找函数indch、数字查找函数indin,会触发编译隐式声明警告,运行时存在不可预期的内存问题。
  • 解码输入终止逻辑完全失效:scanf("%d")读取整数时会自动跳过所有空白字符(包括换行符\n),因此判断txt[j]=='\n'永远不可能成立,循环会持续读取直到数组越界崩溃。
  • 解码输出格式符错误:匹配到对应字符后,使用%d格式输出dec[ind](char类型的字母),会打印字符的ASCII数值,而非字母本身。
  • 索引查找函数存在未定义行为:indch、indin中用于返回结果的indx变量未初始化,如果传入的待查找值不在映射表中,会返回栈空间的随机垃圾值,造成数组越界访问。
  • 编码函数存在冗余代码:arrlen = sizeof txt / sizeof txt[0];计算的是输入缓冲区固定长度100,后续逻辑完全没有使用该变量。
  • 输入缓冲区残留换行问题:菜单选择用scanf读取整数后,输入队列中残留的换行符会干扰后续文本/编码读取,导致输入逻辑异常。
修正后可运行代码
#include<stdio.h>
#include<string.h>

int encode(char dec[], int enc[]);
int decode(int enc[], char dec[]);
int indch(char arr[], char x);
int indin(int arr[], int x);

int main()
{
    int c;
    char dec[] = {'A', 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f', 'G', 'g', 'H', 'h', 'I', 'i', 'J', 'j', 'K', 'k', 'L', 'l', 'M', 'm', 'N', 'n', 'O', 'o', 'P', 'p', 'Q', 'q', 'R', 'r', 'S', 's', 'T', 't', 'U', 'u', 'V', 'v', 'W', 'w', 'X', 'x', 'Y', 'y', 'Z', 'z'};
    int enc[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52}; 
    
    while(1)
    {
        printf("1.Encode\n");
        printf("2.Decode\n");
        printf("3.Exit\n");
        printf("Enter your choice:");
        scanf("%d", &c); 
        getchar(); // 吸收菜单输入残留的换行符
        if(c==1)
        { 
            encode(dec, enc);
            printf("\n");
        }
        else if(c==2)
        {
            decode(enc, dec);
            printf("\n");
        }
        else if(c==3)
        {
            break;
        }
        else{
            printf("Invalid choice!\n");
        }
    }
    
    return 0;  
}

int indch(char arr[], char x)
{
    int arrlen = 52;
    int indx = -1; // 初始化默认返回值,未找到时返回-1用于异常判断
    for(int i=0; i<arrlen; i++)
    {
        if(arr[i] == x)
        {
            indx = i;
            break;
        } 
    }
    return indx;
}

int indin(int arr[], int x)
{
    int arrlen = 52;
    int indx = -1; // 初始化默认返回值
    for(int i=0; i<arrlen; i++)
    {
        if(arr[i] == x)
        {
            indx = i;
            break;
        } 
    }
    return indx;
}

int encode(char dec[], int enc[])
{
    char txt[100];
    int ind;
    char a;
    int i = 0;
    printf("Enter the text to be encoded:");
    scanf("%s", txt); 
    while(txt[i]!= '\0')
    {
        a = txt[i];
        ind = indch(dec , a);
        if(ind != -1) { // 仅处理映射表范围内的合法字符
            printf("%d ",enc[ind]);
        }
        i++;
    }
    return 0;
}

int decode(int enc[], char dec[])
{
    int txt[100];
    int arrlen=0;
    int ind;
    int a;
    printf("Enter the code to be decoded(separate by space, end with Enter):");
    while(1)
    {
        scanf("%d", &txt[arrlen]);
        arrlen++;
        // 读取数字后检查下一个字符是否为换行,是则终止输入
        if(getchar() == '\n' || arrlen >= 100) {
            break;
        }
    }

    for(int i=0; i<arrlen; i++)
    {
        a = txt[i];
        ind = indin(enc, a);
        if(ind != -1) { // 仅处理合法编码值
            printf("%c", dec[ind]); // 用%c格式符输出字符
        }
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者Piranavan R

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 06:27:26