功能:向一个字符数组内任意输入一串字符,然后 统计其中英文大写字母、小写字母、数字、空格 以及其他字符各有多少个。 -------------------------------------------*/ #include<stdio.h> void main() { int uprletter,//大写字符 lwrletter,//小写字符 digit,//数字 space,//空格 others,//其他字符 i; char text[80]; printf("input string:\n"); gets(text); printf("string:"); puts(text); uprletter=0; lwrletter=0; digit=0; space=0; others=0; for(i=0;text[i]!='\0';i++) /**************FILL***************/ if(text[i]>='A'&&text[i]<='_________') uprletter++; /**************FILL***************/ else if(text[i]>='a'&&text[i]<='___________') lwrletter++; /**************FILL***************/ else if(text[i]>='0'&&text[i]<='__________') digit++; else if(text[i]==32) space++; else others++; printf("uprletter:%d ,lwrletter:%d ,digit:%d ,space:%d ,others:%d\n", uprletter,lwrletter,digit,space,others); }
时间: 2023-07-23 07:20:36 浏览: 40
这段代码存在一些问题,我这里给您提供一份修改后的代码:
```c
#include <stdio.h>
int main() {
int uprletter = 0, lwrletter = 0, digit = 0, space = 0, others = 0;
char text[80];
printf("input string:\n");
gets(text);
printf("string:");
puts(text);
for (int i = 0; text[i] != '\0'; i++) {
if (text[i] >= 'A' && text[i] <= 'Z') {
uprletter++;
} else if (text[i] >= 'a' && text[i] <= 'z') {
lwrletter++;
} else if (text[i] >= '0' && text[i] <= '9') {
digit++;
} else if (text[i] == ' ') {
space++;
} else {
others++;
}
}
printf("uprletter:%d, lwrletter:%d, digit:%d, space:%d, others:%d\n",
uprletter, lwrletter, digit, space, others);
return 0;
}
```
修改后的代码主要是:
1. 将 `void main()` 修改为 `int main()`,因为函数 main 的返回值类型应该是 int 类型。
2. 将变量的初始化放到了变量定义的地方。
3. 将 `for` 循环的变量 `i` 的类型从 `void` 改为 `int`。
4. 将每个条件语句的空格、花括号等符号进行了调整。
注意:这里使用了 `gets` 函数读取输入的字符串,但是该函数已经不安全,建议使用 `fgets` 函数代替。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)