8.2 程序將輸入作為字符流讀取,直到遇到EOF。該程序打印每個輸入字符及其ASCLL編碼的十進制。
ASCLL 序列中空格字符前面的字符是非打印字符,需特殊處理。如換行符制表符。可使用控制字符 如 Ctrl+A 顯示為^A, A 的ASCLL值是Ctrl+A的值加64.
除去每次遇到換行符就換行以外,每行打印10對值。
#include<stdio.h> #define CTRL '^' #define NUM 64 int main() { int ch; int n=0; while((ch=getchar())!=EOF) { if(ch=='/n') { PRintf("//n"); printf("%d ",ch); } else if(ch=='/t') { printf("//t"); printf("%d ",ch); } else if(ch<' ') { putchar(CTRL); putchar(ch+64); printf("%d ",ch); } else { putchar(ch); printf("%d ",ch); } n++; if(n%10 == 0) printf("/n"); } return 0;}
int isupper(int c); 判斷字符C是否是大寫字母。 int islower(int c); 判斷字符C是否是小寫字母。
其中空格和標點符號不應該計算。
#include<stdio.h> #include<ctype.h> int main() { int ch; int letter=0; int Word=0; bool isword=false; printf("please input. enter ctrl+z to end!/n"); while((ch=getchar())!=EOF) { if(!isspace(ch) && !ispunct(ch)) //僅統計字符數 不統計空格和特殊字符 letter++; if(!isspace(ch) && !isword && !ispunct(ch))// 到達單詞尾部 { isword=true; word++; } if(isspace(ch) && isword) // 開始新的單詞 isword=false; } printf("%d characters input/n",letter); printf("%d words input/n",word); printf("The average number is %.2f/n",(float)letter/word); return 0;新聞熱點
疑難解答