设计一个C语言程序:由键盘输入一串字符,直到键入回车键时退出;对字母进行大小写转换;若输入的不是字母则报错;并统计出键入的大写字母数、小写字母数和非字母数。
设计一个C语言程序:由键盘输入一串字符,直到键入回车键时退出;对字母进行大小写转换;若输入的不是字母则报错;并统计出键入的大写字母数、小写字母数和非字母数。
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
boonion 2021-11-14 22:58关注#include <stdio.h> #include <stdlib.h> int main(int argc,char *argv[]) { char *str = (char *)malloc(sizeof(char)*100); int ch_max = 0; int ch_min = 0; int ch_other = 0; int i = 0; printf("please entern the str: "); scanf("%[^\n]",str);//带空格输入,scanf("%s",str);不带空格输入 printf("input str = %s\n",str); while(str[i] != '\0') { if( str[i] >= 'a' && str[i] <= 'z')//判断字符为小写 { ch_min ++;//小写统计加一 str[i] = 'A' + (str[i] - 'a');//转换为大写 } else if ( str[i] >= 'A' && str[i] <= 'Z') { ch_max ++; str[i] = 'a' + (str[i] - 'A'); } else ch_other++; i++; } if(ch_other == 0)//如果没有其他字符,正常输出,否则错误输出 printf("output str = %s\n",str); else printf("input error, ch_min = %d , ch_max = %d ,ch_other = %d\n",ch_min,ch_max,ch_other); return 0 ; }
解决 无用评论 打赏 举报 编辑记录