[問題] 拿掉字串中的空白字元

看板C_and_CPP (C/C++)作者 (要不回那些快樂)時間15年前 (2011/04/15 16:57), 編輯推噓1(101)
留言2則, 1人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Dev-C++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 例如當我輸入字串abc def時 想要拿掉空白字元 但是出來的結果會變成只有abc 後面的def不見了 餵入的資料(Input): abc def 預期的正確結果(Expected Output): abcdef 錯誤結果(Wrong Output): abc 程式碼(Code):(請善用置底文網頁, 記得排版) #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char a[10],b[10]; int len,i,j; gets(a); len=strlen(a); j=0; for(i=0;i<len;i++) { if(a[i]!=' ') { b[j]=a[i]; j++; } } b[j]='\0'; printf("%s\n",b); system("pause"); return 0; } 補充說明(Supplement): 請問是哪邊需要做修改才能顯示正確的結果? -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 125.227.68.178

04/15 16:59, , 1F
b["j"]=a[i].....
04/15 16:59, 1F

04/15 17:10, , 2F
for迴圈跑完後應該是b[j]=0
04/15 17:10, 2F
剛有地方不小心打錯了 現在更正之後 已可正確顯示結果 謝謝各位 ※ 編輯: painfullove 來自: 125.227.68.178 (04/15 17:14)
文章代碼(AID): #1Dg0YK5n (C_and_CPP)
文章代碼(AID): #1Dg0YK5n (C_and_CPP)