在C中使用不同的库

编程入门 行业动态 更新时间:2024-10-25 14:32:49
本文介绍了在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中使用不同的库

本文发布于:2023-11-13 06:40:21,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!