當前位置:首頁 » 編程語言 » c語言統計字元數

c語言統計字元數

發布時間: 2025-06-21 15:07:40

c語言 統計字元個數

要統計英文字母,空格,數字和其他字元的個數,代碼如下:
#include<stdio.h>

#include<stdlib.h>
int main()
{
char c;
int letters=0;
int space=0;
int digit=0;
int other=0;
printf("請輸入一行字元:>");
while((c=getchar())!='\n')
{
if((c>='a'&&c<='z')||(c>='A'&&c<='Z'))
{
letters++;
}
else if(''==c)
{
space++;
}
else if(c>='0'&&c<='9')
{
digit++;
}
else
{
other++;
}
}
printf("字母的個數:>%d\n空格的個數:>%d\
\n數字的個數:>%d\n其他字元的個數:>%d\n",\
letters,space,digit,other);
system("pause");
return 0;
}

❷ 用c語言編程,對輸入的一行字元進行統計分析,要求統計並輸出出現的數字字元及其個數(用冒號分隔)

C代碼和運行結果如圖:

輸出符合樣例,望採納~

源碼

#include <stdio.h>

int main() {

char s[100]; // 輸入不超過100字元

int i = 0, cnt[10] = {0}; // 0-9每個數字字元個數統計

fgets(s, 100, stdin); // 讀入一行字元,包括換行符' '

while (s[i] != ' ') { // 遇到換行即到結尾,改成空字元''也可以

if (s[i] >= '0' && s[i] <= '9') // 數字字元

cnt[s[i] - '0']++; // 統計對應字元個數

i++;

}

for (i = 0; i < 10; i++) { // 輸出出現的數字字元個數

if (cnt[i] > 0)

printf("%d: %d ", i, cnt[i]);

}

return 0;

}

熱點內容
oracle測試sql 發布:2025-07-08 03:16:54 瀏覽:973
php壁紙源碼 發布:2025-07-08 03:04:26 瀏覽:320
android應用層 發布:2025-07-08 02:42:32 瀏覽:301
大唐存儲銷量 發布:2025-07-08 02:41:11 瀏覽:582
腳本怎麼打開 發布:2025-07-08 02:41:06 瀏覽:822
貴州電信iPtv升級伺服器地址 發布:2025-07-08 02:38:48 瀏覽:412
電腦怎麼鏈接本地伺服器 發布:2025-07-08 02:34:22 瀏覽:147
android調試webview 發布:2025-07-08 02:26:28 瀏覽:358
壓縮袋鞋子 發布:2025-07-08 02:21:30 瀏覽:752
為什麼安卓打吃雞感覺有延遲 發布:2025-07-08 02:09:32 瀏覽:168