按以下要求编写程序,且必须用数组、函数调用及全局变量。有一篇文章,共有三行文字,每行有80个字符。要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数。并用c语言写出来
时间: 2024-02-03 22:13:22 浏览: 27
下面是符合要求的C语言程序:
```c
#include <stdio.h>
int upper = 0; // 全局变量,用于记录英文大写字母数量
int lower = 0; // 全局变量,用于记录英文小写字母数量
int digit = 0; // 全局变量,用于记录数字数量
int space = 0; // 全局变量,用于记录空格数量
int other = 0; // 全局变量,用于记录其他字符数量
void count(char str[]) {
int 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') { // 判断是否为数字
digit++;
} else if (str[i] == ' ') { // 判断是否为空格
space++;
} else { // 其他情况都视为其他字符
other++;
}
i++;
}
}
int main() {
char str1[81] = "This is a test for counting characters in a string, including Upper and lower case letters, digits, spaces and other characters.";
char str2[81] = "This is the second line, and it also contains Upper and lower case letters, digits, spaces and other characters.";
char str3[81] = "This is the third and last line, which has similar characters as the previous two lines.";
count(str1); // 统计第一行字符数量
count(str2); // 统计第二行字符数量
count(str3); // 统计第三行字符数量
printf("Upper case letters: %d\n", upper);
printf("Lower case letters: %d\n", lower);
printf("Digits: %d\n", digit);
printf("Spaces: %d\n", space);
printf("Other characters: %d\n", other);
return 0;
}
```
程序中定义了五个全局变量,分别用于记录英文大写字母、小写字母、数字、空格以及其他字符的个数。使用函数 `count` 统计字符串中各种字符的数量,并在主函数中调用三次,分别统计三行字符串中的字符数量。最后输出各种字符的数量即可。