2012-10-25 40 views
2
for (i = 0; i < size; i++) { 
if((string[i] >= 65 && string[i] <= 90) || (string[i] >= 97 && string[i] <= 122) || string[i] == 10) { 
    sec_str[j] = tolower(string[i]); 
    putchar(sec_str[j]); 
    j++; 
} 
} 
printf("%s\n", sec_str); 

這是我的代碼,試圖以一個字符串複製到另一個,剝離所有非字母的情況下,這對我來說工作得很好,我用的putchar(sec_str [J]。)檢查和它們都很好,但是當我檢查printf(「%s」,sec_str)時,輸出結果很亂。是這樣的:C程序串亂

asantaspotstopsatnasa 
asantaspotstopsatnasa 

twasbrilligandtheslithytoves 
twasbrilligandtheslithytoves 
����r�$ 
yobananaboy 
yobananaboy 
ndth 
neveroddoreven 
neveroddoreven 
h 
thetimehascomethewalrussaid 
thetimehascomethewalrussaid 
����t�r�$ 

和printf的應打印

asantaspotstopsatnasa 
twasbrilligandtheslithytoves 
yobananaboy 
neveroddoreven 
thetimehascomethewalrussaid 

是正確

回答

4

你忘了一個空終止字符串添加:)

#include <stdio.h> 
#include <ctype.h> 
... 
    for (i = 0; i < size; i++) { 
    if(ischar(string[i]) || (string[i] == 10)) { 
     sec_str[j] = tolower(string[i]); 
     putchar(sec_str[j]); 
     j++; 
    } 
    } 
    sec_string[j] = '\0'; 
    printf("%s\n", sec_str); 
結束
+0

工作的OMG!謝謝 – user1773037