本文介绍了在C中使用不同的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从头开始编写一个程序,可以计算用户输入的大小写字母和空格。从那时起,我发现那些特定函数的代码已经在另一个库中被预先编写了!我的问题是,如何使用
isupper(int c), islower(int c),和 isspace(int c) C>文件ctype.h 。
#include< stdio.h> int main(void){ int iochar,numdigits = 0,numlower = 0,numupper = 0,numwhites = 0; printf(请输入一个短语:\\\\\\); ((iochar = getchar())!= EOF) if((iochar =='')||(iochar =='\ t' )||(iochar =='\\\')) { numwhites ++; putchar(iochar); ((iochar> ='0')&&(iochar< ='9')) { numdigits ++; } else if putchar(iochar); (('''= iochar)&&(iochar< ='z')) { numlower ++; } else putchar(iochar-32); ((A'< = iochar)&&(iochar< ='Z')) { numupper ++; } else if putchar(iochar); } else putchar(iochar); } printf(%d个白色字符,%d个数字,numwhites,numdigits); printf(%d小写字母已被转换为,numlower); printf(大写和%d大写。),numupper); printf(\\\\\\); return 0;}解决方案 $ p $ b #include< stdio.h> #include< ctype.h> $ b $ int main(int argc,char ** argv){ int iochar,numdigits = 0,numlower = 0,numupper = 0,numwhites = 0; printf(请输入一个短语:\\\\\\); ((iochar = getchar())!= EOF){ //在必要时增加计数 if(isspace(iochar)){ numwhites ++; } else if(isdigit(iochar)){ numdigits ++; } else if(islower(iochar)){ numlower ++; iochar = toupper(iochar); } else if(isupper(iochar)){ numupper ++; } //总是发生这种情况,不要把它放在if putchar(iochar)中; } printf(%d个白色字符,%d个数字,numwhites,numdigits); printf(%d小写字母已被转换为,numlower); printf(大写和%d大写。),numupper); printf(\\\\\\); 返回0; }
I just coded from scratch a program that would count the upper and lowercase letters and blank spaces of whatever the user input. Since then I found out that code for those specific functions have already been prewritten in another library! My question is, how can all the code that I have written below be simplified with the use of
isupper(int c), islower(int c), and isspace(int c) which are defined in ctype.h.
#include <stdio.h> int main(void){ int iochar, numdigits=0, numlower=0, numupper=0, numwhites=0; printf("Please enter a phrase:\n\n"); while((iochar=getchar())!=EOF) { if ((iochar==' ')||(iochar=='\t')||(iochar=='\n')) { numwhites++; putchar(iochar); } else if((iochar>='0')&&(iochar<='9')) { numdigits++; putchar(iochar); } else if(('a'<=iochar)&&(iochar<='z')) { numlower++; putchar(iochar-32); } else if(('A'<=iochar)&&(iochar<='Z')) { numupper++; putchar(iochar); } else putchar(iochar); } printf("%d white characters, %d digits, ",numwhites,numdigits); printf("%d lowercase have been converted to ",numlower); printf("uppercase and %d uppercase.\n",numupper); printf("\n\n"); return 0;}解决方案
Here is it written better and cleaned up:
#include <stdio.h> #include <ctype.h> int main(int argc, char **argv) { int iochar, numdigits=0, numlower=0, numupper=0, numwhites=0; printf("Please enter a phrase:\n\n"); while ((iochar=getchar()) != EOF) { // increase counts where necessary if (isspace(iochar)) { numwhites++; } else if (isdigit(iochar)) { numdigits++; } else if (islower(iochar)) { numlower++; iochar = toupper(iochar); } else if (isupper(iochar)) { numupper++; } // this happens always, don't put it in the if's putchar(iochar); } printf("%d white characters, %d digits, ", numwhites, numdigits); printf("%d lowercase have been converted to ", numlower); printf("uppercase and %d uppercase.\n", numupper); printf("\n\n"); return 0; }
更多推荐
在C中使用不同的库
发布评论