getchar()解決“輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數”


  getchar()函數的作用是從計算機終端(一般為鍵盤)獲取一個無符號字符。getchar()函數只能接收一個字符,其函數值就是從輸入設備獲取到的字符。

該函數聲明在stdio.h頭文件中,使用的時候要包含stdio.h頭文件。

如:

  #include<stdio.h>
  int getchar(void);
 例:

 1 #include "stdio.h" 2 main() {
 3     char c;   
 4     int letters=0,space=0,digit=0,others=0;  
 5     printf("please input some characters\n");  
 6     while((c=getchar())!='\n')  { 
 7         if(c>='a'&&c<='z'||c>='A'&&c<='Z')   letters++;  elseif(c=='')   space++;     
 8         elseif(c>='0'&&c<='9')        digit++;      
 9         else         others++;
10     } 
11     printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,         space,digit,others);
12 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM