當前位置:首頁 » 編程語言 » c語言空格函數

c語言空格函數

發布時間: 2024-02-26 05:27:32

c語言里如何判斷空格

代碼如下:

int main(void) {

char c;

printf("請輸入要測試的字元 ");

scanf("%c",&c);

if(c == ' '){

printf("是空格 ");

}else{

printf("不是空格 ");

}

return 0;

}

C語言對字元判斷是否為空格有兩種方式:

  1. 代碼:if(c=='')printf("c是空格");這種方法比較直觀,直接比較當前字元和空格是不是相等,是的話就是空格了。代碼演示如下:

㈡ c語言去掉字元串的空格函數trim

c語言去掉字元串的空格函數 void trim(char *s){} 如下:
#include <stdio.h>
void trim(char *s){
int i,L;
L=strlen(s);
for (i=L-1;i>=0;i--) if (s[i]==' ')strcpy(s+i,s+i+1);
}
int main(){
char s[100];
printf("input 1 line string\n");
gets(s);
trim(s);
printf("%s\n",s);
return 0;
}
例如:
input 1 line string
abc 123 XYZ |
輸出:abc123XYZ|

㈢ c語言如何輸出有空格的字元串

c語言中輸出字元串的函數有printf("%s")
puts()
fputs()等,字元串中可以是任意的字元,包括空格在內,無特殊處理,如:
char str[]="hello world" ;
printf("%s\n", str );
puts(str);
fputs(str,stdin);
但在輸入帶有空格的字元串時,只能用gets()或fgets(),而不能用scanf("%s")
,因為scanf("%s")輸入字元串時,遇到空格就結束了輸入。而gets()函數是以回車為結束符的輸入函數,可以輸入帶空格的字元串。

熱點內容
百兆縱向加密 發布:2025-09-08 05:54:39 瀏覽:746
我的世界租伺服器需要哪些 發布:2025-09-08 05:54:10 瀏覽:260
phpfileput 發布:2025-09-08 05:34:36 瀏覽:320
androiduipdf 發布:2025-09-08 04:36:24 瀏覽:527
問道手游腳本在哪買 發布:2025-09-08 04:24:04 瀏覽:27
安卓怎麼修 發布:2025-09-08 04:23:57 瀏覽:944
伺服器網卡與ip是否應該一致 發布:2025-09-08 04:19:40 瀏覽:200
安卓開發imageview是什麼控制項 發布:2025-09-08 04:15:52 瀏覽:135
sqlservertcpip 發布:2025-09-08 03:53:00 瀏覽:314
sql語句匯總 發布:2025-09-08 03:42:23 瀏覽:837