c語言輸入一行字串,如何統計其中的字母和數字的個數

時間 2021-08-30 10:53:59

1樓:問明

要統計英文字母,空格,數字和其他字元的個數,**如下:

#include

#include

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;

include用法:

#include命令預處理命令的一種,預處理命令可以將別的源**內容插入到所指定的位置;可以標識出只有在特定條件下才會被編譯的某一段程式**;可以定義類似識別符號功能的巨集,在編譯時,前處理器會用別的文字取代該巨集。

插入標頭檔案的內容

#include命令告訴前處理器將指定標頭檔案的內容插入到前處理器命令的相應位置。有兩種方式可以指定插入標頭檔案:

1、#include《檔名》

2、#include"檔名"

如果需要包含標準庫標頭檔案或者實現版本所提供的標頭檔案,應該使用第一種格式。如下例所示:

#include//一些數學函式的原型,以及相關的型別和巨集

如果需要包含針對程式所開發的原始檔,則應該使用第二種格式。

採用#include命令所插入的檔案,通常副檔名是.h,檔案包括函式原型、巨集定義和型別定義。只要使用#include命令,這些定義就可被任何原始檔使用。如下例所示:

#include"myproject.h"//用在當前專案中的函式原型、型別定義和巨集

你可以在#include命令中使用巨集。如果使用巨集,該巨集的取代結果必須確保生成正確的#include命令。例1展示了這樣的#include命令。

【例1】在#include命令中的巨集

#ifdef _debug_

#define my_header"myproject_dbg.h"

#else

#define my_header"myproject.h"

#endif

#include my_header

當上述程式**進入預處理時,如果_debug_巨集已被定義,那麼前處理器會插入myproject_dbg.h的內容;如果還沒定義,則插入myproject.h的內容。

2樓:阿四哥

c語言字串的學習,輸入指定字串,並且計算字串的位數

3樓:1024程式設計師

c語言經典例子之統計英文、字母、空格及數字個數

4樓:聽不清啊

用下面的迴圈加判斷就可以統計其中的字母和數字的個數:

int i,zm=0,sz=0;

char s[200];

gets(s);

for(i=0;s[i];i++)

printf("共有字母%d 數字%d\n",zm,sz);

5樓:安志鋼

定義一個陣列,存字串。然後用迴圈,依次比較陣列中每一個和字母的ascii碼範圍比較,在這個範圍內,就是字母,字母數量統計變數加一(初值為1);如果不是字母,再和數字ascii碼進行比較,如果在範圍內,數字個數統計變數加1(同樣,初值賦值1)。思路就是這樣的了。

6樓:

#include

#include

int main()

printf("date_num = %d\n",date_num);

printf("str_num = %d\n",str_num);

return 0;}

7樓:一條街不

#include

#include

#include

int main()

e++;

}printf("%d\n",c);

c=0;

e=0;

}return 0;}

8樓:

#include

void main()

printf("the data number = %d\r\n",num_data); // 列印出字串中數字的個數

printf("the char number = %d\r\n",num_char); // 列印數字串中字母的個數

}一字一字敲的,還驗證了一遍,絕對可行。希望你幫到你!

9樓:love訫

#include

main()

else if(ch==' ')

else if(ch>='0'&&ch<='9')else

}printf("字母= %d,空格= %d,數字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);

return 0;}

c語言:輸入一行字元,分別統計出其中英文字母、空格、數字和其他字元的個數

10樓:折柳成萌

一、問題分析:

輸入一行字母,那麼會以換行結束。所以可以存入陣列,也可以逐個輸入,遇到換行結束。

要統計各個類的個數,就要逐個判斷是哪個分類的。

由於在ascii碼中,數字,大寫字母,小寫字母分別連續,所以可以根據邊界值判斷型別。

二、演算法設計:

1、讀入字元,直到遇到換行結束。

2、對於每個字元,判斷是字母還是數字,或者空格,或者是其它字元。

3、對於每個字元判斷後,對應類別計數器自加。

4、最終輸出結果。

三、參考**:

#include int main() printf("%d %d %d %d\n", a,b,c,d);//輸出結果。 return 0;}

11樓:啊啊啊的帝國號

main()

printf("字母=%d,數字=%d,空格=%d,其他=%d\n",zm,sz,kg,qt);}

12樓:程式設計師的每一天

c語言經典例子之統計英文、字母、空格及數字個數

13樓:幸福de小陽

你好:你定義字元陣列 char a[50] 時,意味著這個字串最多能容納 50 個字元,並不是說一定是 50 個字元。(實際上是 49 個字元,因為作為字串要有一個結束標識 '\0' 的)

你可以藉助 string.h 標頭檔案裡的 strlen 函式來測量字串的長度。

程式修改如下:

#include

#include

void main()

else if(c>='0' && c<='9')else if(c==' ')

else

}printf("letter: %d, number: %d, blank: %d, other: %d\n",letter,number,blank,other);}

14樓:匿名使用者

#include

void main()

printf ("字母

:%d\n", letter);

printf ("空格:%d\n", space);

printf ("數字:%d\n", digit);

printf ("其它字元:%d\n", other);}

15樓:胡敏鋒

#include

int main()

printf("字母數:

%d\n空格數:%d\n數字數:%d\n其他字元數:%d\n",letters,space,digit,other);

return 0;}

16樓:匿名使用者

給個推薦 謝謝!

static void main(string args)console.writeline("字母有"+ch+"個,空格有"+sp+"個,數字有"+math+"個,其它字元有"+other+"個.");

console.readline();}

17樓:希伯來爵

for(i=0;i<50;i++)

{if(c=='\0')

break;////////////////////////你未必會輸滿50個字元吧,所以遇到'\0'就要結束迴圈

if((c>='a'&&c<='z')||(c>='a'&&c<='z'))

letter++;

18樓:倒黴熊

#include

#include

#include

int i,a[4];

char t;

int main()

for(i=0;i<4;i++) printf("%d ",a[i]);

system("pause");

return 0;

}//看看這個行不行,我執行可以。

19樓:匿名使用者

#include

#include

int main()

return 0;}

20樓:匿名使用者

#include

int main()

printf("%d, %d, %d, %d other", numcnt, albcnt, spacecnt, numcnt);

return 0;}

21樓:自v定v義

#include

void main()

printf("num:%d, ch:%d, space:%d,other: %d", num,ch, space,other);}

22樓:匿名使用者

||用for語句編的.....

#include

void main()

printf("zimu:%d\nspace:%d\nshuzi:%d\nqita:%d\n"z,k,s,q);}

23樓:匿名使用者

#include

#include

#define a 80

main()

printf("英文字元有:%d\n",letter);

printf("數字字元有:%d\n",digit);

printf("空格有:%d\n",space);

printf("其他字元有:%d\n",others);}

c語言 輸入一行字串,統計字母,數字和其它符號的個數

24樓:四舍**入

#include

int main()

{char s[200];

int i,zm=0,sz=0,qt=0;

for(i=0;s[i];i++)

if(s[i]>='a'&&s[i]<='z'||s[i]>='a'&&s[i]<='z')zm++;

else if(s[i]>='0'&&s[i]<='9')sz++;

else qt++;

return 0;

C陣列輸入一行字串(長度小於字元,只有字母和

珈藍惜夢 源程式 以及演算法解釋如下 define crt secure no warnings vs環境下需要,vc不需要 include include int main 字元陣列 int numb count 0 數字個數 int abc count 0 大寫字母個數 int abc coun...

運用C 如何讀取一行字串的指定字元

泡影果果 從檔案中讀取資料,一行一行的讀取,用getline include fstream in cstring strfilename e 測試 a.txt 路徑是雙斜槓 in.open strfilename,ios in ios base in while in.eof 如果沒有讀到檔案結尾...

C語言 輸入字串,然後逆序輸出

橘落淮南常成枳 可以將整數當做字串 字串長度不超過10 接收,然後反向輸出字元陣列元素即可。字串實際長度可以用strlen函式來計算。方法程式如下 include include main 杜哥是個小天才 include int main int len,i char str 100 gets st...