使用c语言的三种循环分别写出输入一行字符统计大小写英文字母,空格,数字和其他字符的个数的代码如下:
**1. for循环实现**
```c
#include <stdio.h>
int main()
{
char str[100];
int i, upper, lower, digits, spaces, others;
upper = lower = digits = spaces = others = 0;
printf("请在此输入字符串: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
upper++;
}
else if (str[i] >= 'a' && str[i] <= 'z')
{
lower++;
}
else if (str[i] >= '0' && str[i] <= '9')
{
digits++;
}
else if (str[i] == ' ')
{
spaces++;
}
else
{
others++;
}
}
printf("大写字母数量: %d\n", upper);
printf("小写字母数量: %d\n", lower);
printf("数字数量: %d\n", digits);
printf("空格数量: %d\n", spaces);
printf("其他字符数量: %d\n", others);
return 0;
}
```
**2. while循环实现**
```c
#include <stdio.h>
int main()
{
char str[100];
int i, upper, lower, digits, spaces, others;
upper = lower = digits = spaces = others = 0;
printf("请在此输入字符串: ");
fgets(str, sizeof(str), stdin);
i = 0;
while (str[i] != '\0')
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
upper++;
}
else if (str[i] >= 'a' && str[i] <= 'z')
{
lower++;
}
else if (str[i] >= '0' && str[i] <= '9')
{
digits++;
}
else if (str[i] == ' ')
{
spaces++;
}
else
{
others++;
}
i++;
}
printf("大写字母数量: %d\n", upper);
printf("小写字母数量: %d\n", lower);
printf("数字数量: %d\n", digits);
printf("空格数量: %d\n", spaces);
printf("其他字符数量: %d\n", others);
return 0;
}
```
**3. do-while循环实现**
```c
#include <stdio.h>
int main()
{
char str[100];
int i, upper, lower, digits, spaces, others;
upper = lower = digits = spaces = others = 0;
printf("请在此输入字符串: ");
fgets(str, sizeof(str), stdin);
i = 0;
do
{
if (str[i] >= 'A' && str[i] <= 'Z')
{
upper++;
}
else if (str[i] >= 'a' && str[i] <= 'z')
{
lower++;
}
else if (str[i] >= '0' && str[i] <= '9')
{
digits++;
}
else if (str[i] == ' ')
{
spaces++;
}
else
{
others++;
}
i++;
} while (str[i] != '\0');
printf("大写字母数量: %d\n", upper);
printf("小写字母数量: %d\n", lower);
printf("数字数量: %d\n", digits);
printf("空格数量: %d\n", spaces);
printf("其他字符数量: %d\n", others);
return 0;
}
```