具體代碼如下所述:
#include<stdio.h>void main(){ int c, letter=0, num=0, blank=0, tab=0, enter=0, other=0 ,i=0, sum=0; printf("Please input a string:/n"); while((c=getchar())!=EOF){ sum++; if(c==' '){ ++blank; //空格鍵的個數 } else if(c=='/t'){ ++tab; //Tab鍵的個數 } else if(c=='/n'){ ++enter; //回車鍵的個數 } else if((c>='A' && c<='Z') || (c>='a' && c<='z')){ ++letter; //字母的個數 } else if(c>='0' && c<='9'){ ++num; //數字的個數 } else ++other; //其他字符的個數 i++; } printf("There are %d characters/n", sum); printf("blank=%d, Tab=%d, Enter=%d, letter=%d, number=%d ,other=%d/n",blank, tab, enter, letter, num, other);}
結果為:
總結
以上所述是小編給大家介紹的C語言統計一串字符中空格鍵、Tab鍵、回車鍵、字母、數字及其他字符的個數(Ctrl+Z終止輸入) ,希望對大家有所幫助,如果大家有任何疑問歡迎
新聞熱點
疑難解答