我有这个小程序,我需要用字母表替换我输入的任何字符串,所以a = 01,b = 02,n = 14,7 = 07 ...例如,如果我输入ab36c作为输出我应该得到01 02 03 06 03
当我在另一台计算机上编译这一切时,一切正常,现在当我在我的电脑程序崩溃时运行它,我仍然可以输入我的字符串,但当我按Enter键获得结果(输出)时,它显示program.exe已停止工作.什么错了?
#include#include #include #include #include #include //#define SIMBOLU_SKAITS 100 int main(){ char text[200]; char *s2; char simboli[36]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t', 'u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9'}; char morze[36][3]={"01","02","03","04","05","06","07","08","09","10","11","12","13", "14","15","16","17","18","19","20","21","22","23","24","25","26", "00","01","02","03","04","05","06","07","08","09"}; int i, j, garums; gets(text); garums=strlen(text); for (i=0;i<=garums;i++){ for (j=0; j<=36;j++) if( text[i]==simboli[j]){ strcat(s2,morze[j]); strcat(s2," "); break; } } puts(s2); scanf("%c"); }
Jabberwocky.. 5
你做strcat(s2,morze[j]);
但s2
从未初始化,因此它最有可能指向无效的内存,因此崩溃.
编辑:
...而且scanf("%c")
因为你没有提供参数而崩溃.你需要:
char c ; scanf("%c", &c) ;
EDIT2:
这是没有使用simboli
和morze
数组的版本:
char *outp = s2 ; for (i = 0; i <= garums; i++) { char c = text[i] ; if (c >= 'a' && c <= 'z') outp += sprintf(outp, "%02d ", c - 'a' + 1) ; else if (c >= '0' && c <= '9') outp += sprintf(outp, "%02d ", c - '0') ; }
EDIT3
总结一下:
替换:char *s2 ;
用char s2[200];
和替换scanf("%c") ;
用scanf("%c", &c) ;
你做strcat(s2,morze[j]);
但s2
从未初始化,因此它最有可能指向无效的内存,因此崩溃.
编辑:
...而且scanf("%c")
因为你没有提供参数而崩溃.你需要:
char c ; scanf("%c", &c) ;
EDIT2:
这是没有使用simboli
和morze
数组的版本:
char *outp = s2 ; for (i = 0; i <= garums; i++) { char c = text[i] ; if (c >= 'a' && c <= 'z') outp += sprintf(outp, "%02d ", c - 'a' + 1) ; else if (c >= '0' && c <= '9') outp += sprintf(outp, "%02d ", c - '0') ; }
EDIT3
总结一下:
替换:char *s2 ;
用char s2[200];
和替换scanf("%c") ;
用scanf("%c", &c) ;