C语言加密程序打印数字数组出现异常符号问题求助
加密程序打印数字数组时出现表情符号的问题解决
我正在编写一个加密程序,功能为:先将输入消息的每个字母转换为对应数字(基于给定的5×6字符数组),再结合关键词对数字进行加密后以数组形式打印。但在encryptmessage2函数中打印数字数组时,未显示正确数字,反而出现了表情符号。
程序代码
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h> #define MAXSIZE 50 void encryptmessage1(char[][6]); void encryptmessage2(char[][6]); char temparr[MAXSIZE] = {0}; int length ; int main() { char array[5][6] = { {'A', 'B', 'C', 'D', 'E'}, {'F', 'G', 'H', 'I', 'J'}, {'K', 'L', 'M', 'N', 'O'}, {'P', 'Q', 'Z', 'R', 'S', 'T'}, {'U', 'V', 'W', 'X', 'Y'} }; int choice; printf( "********************************\n" "**ENCRYPTION/DECRYPTION SYSTEM**\n" "********************************\n" "1. Code the message\n" "2. Decode the message\n" "3. EXIT\n" "Make your choice: " ); scanf("%d",&choice); while (choice != EOF) { switch (choice) { case 1: encryptmessage1(array); encryptmessage2(array); break; case 2: // decrypt message break; default: break; } return 0; } } void encryptmessage1(char array[][6]) { int i, j, loop, k = 0, row, col, len = 0; char str[MAXSIZE] = {0}; printf("Please enter your message : "); fgets(str,MAXSIZE,stdin); while ((str[k] = getchar()) != '\n') { k++; } for (loop = 0; loop < MAXSIZE; loop++) { temparr[loop] = str[loop]; } len = sizeof(str) / sizeof(str[0]); for (i = 0; i < len; i++) { if (str[i] == ' ') { for (j = i; j < len; j++) { str[j] = str[j + 1]; } len--; } } i = 0; while (str[i] != '\n') { if (islower(str[i])) { str[i] = toupper(str[i]); } i++; } puts(str); i = 0; k = 0; while (str[k] != '\n') { for (row = 0; row < 5; row++) { for (col = 0; col < 6; col++) { if (str[k] == array[row][col]) { temparr[i] = '0' + row; temparr[i + 1] = '0' + col; i += 2; } } } k++; } length=i; puts(temparr); } void encryptmessage2(char array[][6]) { int i, j,k =0,row,col; char key[MAXSIZE] = {0};// keyword char temp; char numarray[MAXSIZE][MAXSIZE]; printf("Please enter your key : "); while ((temp = getchar()) != '\n') { key[k]=temp; k++; } i = 0; while (key[i] != '\n') { if (islower(key[i])) { key[i] = toupper(key[i]); } i++; } for(i = 0; key[i]; i++) { putchar(key[i]); putchar(' '); } putchar('\n'); int size = i; i=0; k=0; while(i<length) { for(j=0;j<size;j++){ numarray[k][j]=temparr[i]; i++; if(i<length) break; } k++; } int columns = k; for(i=0;i<size;i++) { for(j=0;j<columns;j++) { printf("%c",numarray[i][j]); } printf("\n"); } }
输出情况
- 预期输出:按关键词长度拆分数字数组,每行对应关键词位置的数字,数字字符正常显示
- 实际输出:打印出表情符号等异常字符,无正确数字显示
问题根源
- 数组填充逻辑错误:内层循环的
break条件错误,导致提前终止列填充,大量数组位置未初始化,残留内存垃圾值,这些值的ASCII码恰好对应表情符号。 - 打印循环索引颠倒:打印时的行、列索引与数组填充的索引不匹配,读取了未正确赋值的数组区域。
- 未初始化数组:
numarray未初始化,未赋值的位置保留随机垃圾数据。
修正后的encryptmessage2函数
void encryptmessage2(char array[][6]) { int i, j, k = 0, row, col; char key[MAXSIZE] = {0};// keyword char temp; char numarray[MAXSIZE][MAXSIZE] = {0}; // 初始化数组,避免垃圾值 printf("Please enter your key : "); // 读取关键词,避免越界 while ((temp = getchar()) != '\n' && k < MAXSIZE-1) { key[k] = temp; k++; } key[k] = '\0'; // 确保字符串以结束符结尾 i = 0; // 转换为大写,用字符串结束符判断循环终点 while (key[i] != '\0') { if (islower(key[i])) { key[i] = toupper(key[i]); } i++; } // 打印关键词(带空格) for(i = 0; key[i]; i++) { putchar(key[i]); putchar(' '); } putchar('\n'); int size = i; // 关键词长度 i = 0; k = 0; // 按列填充数组:每个关键词字符对应一行数字 while(i < length) { for(j = 0; j < size && i < length; j++) { numarray[j][k] = temparr[i]; i++; } k++; } int columns = k; // 总列数 // 按行打印数字数组 for(i = 0; i < size; i++) { for(j = 0; j < columns; j++) { if(numarray[i][j] != '\0') { // 只打印有效字符 printf("%c", numarray[i][j]); } } printf("\n"); } }
额外修正点
- 移除
main函数中多余的闭合大括号 - 修正
encryptmessage1的输入逻辑:删除重复读取输入的代码,用strcspn去掉fgets读取的换行符,避免输入混乱 - 替换
encryptmessage1中len = sizeof(str)/sizeof(str[0])为len = strlen(str),正确获取字符串实际长度
内容的提问来源于stack exchange,提问作者XIz
相关产品推荐
相关产品推荐

