當前位置:首頁 » 編程語言 » 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;

}

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:593
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:888
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:581
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:765
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:684
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1013
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:255
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:114
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:806
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:713