방금 C 언어를 배우기 시작했습니다. 내 코드는 문자열에서 짧은 형태의 알파벳 (a-z 또는 A-Z) 또는 숫자 (0-9)를 찾은 다음이를 확장합니다 (abcd..z 등).왜 C에서 문자열 끝에 이상한 기호가 추가 되었습니까?
하지만 문제가 발생했습니다. 문자열 끝에 이상한 기호가 추가되었습니다. 이걸 알아낼 수있게 도와 주실 수 있겠습니까?
코드와 결과를 참조하십시오 : http://codepad.org/4sfBZv48
코드 :
당신은 당신의 문자열에서 터미네이터를위한 공간을 떠날 필요가#include <stdio.h>
#include <stdlib.h>
int main()
{
char str[16] = "--g-6hA-Za-z56-9";
char exstr[127];
int lasti = 0;
int i;
int j;
int start;
for(i = 0; str[i] != 0; i++){
if(str[i] > 64 && str[i] < 91 && str[i+1] == '-' && str[i+2] > 64 && str[i+2] < 91){
}else if(str[i] > 96 && str[i] < 123 && str[i+1] == '-' && str[i+2] > 98 && str[i+2] < 123){
}else if(str[i] > 47 && str[i] < 58 && str[i+1] == '-' && str[i+2] > 47 && str[i+2] < 58){
}else{
exstr[lasti++] = str[i];
continue;
}
start = i;
i += 2;
for(j = str[start]; j <= str[i]; j++)
exstr[lasti++] = j;
}
printf("%s\n",exstr);
return 0;
}
'exstr [lasti] = 0; 'printf' 호출 전에. – ach
이 문자열 exstr의 초기화를 볼 수 없으며 실제로는 exstr [lasti] = '\ 0'이 (가) –
BTW'str [i + 2] '배열의 범위를 초과 할 수 있습니다. – BLUEPIXY