程序改错 功能:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 #include <stdio.h> void main(void) { char str[80]; int i; int letters=0,space=0,digit=0,others=0; printf("please input a string:\n"); get(str); for(i=0;str[i]!='\0';i++) { if(str[i]>='a'&&str[i]<=z'&&str[j]>='A'&&str[i]<=Z) letters++; else if(str[i]=!'") space++; else if(str[i]>='0'&&str[i]<='9') digit++; else others++; } printf("char=%d space=%d digit=%d others=%d\n",letters,space,digit,others); }
时间: 2023-12-03 07:42:04 浏览: 50
程序有以下几处错误:
1. `get()` 函数应该改为 `gets()` 函数。
2. 第10行的 `str[j]` 应该改为 `str[i]`。
3. 第12行的判断条件应该是 `str[i]!=' ' && str[i]!= '\t'`,即判断字符不是空格也不是制表符。
4. 第13行的判断条件应该是 `str[i]>='0'&&str[i]<='9'`,即判断字符是数字。
修改后的程序如下所示:
```c
#include <stdio.h>
void main(void) {
char str[80];
int i;
int letters=0,space=0,digit=0,others=0;
printf("please input a string:\n");
gets(str);
for(i=0;str[i]!='\0';i++) {
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
letters++;
else if(str[i]!=' ' && str[i]!= '\t')
space++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else
others++;
}
printf("char=%d space=%d digit=%d others=%d\n",letters,space,digit,others);
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)