【2015年山西大学考研真题】输入一行字符,分别统计其中英文字母、空格、数字和其他字符的
个数。
(1)算法的基本思想如下:
1. 遍历输入的字符串,依次判断每个字符的类型。
2. 判断字符的类型,根据字符的ASCII码范围来判断是否为英文字母、数字、空格或其他字符。
3. 统计每种字符类型的个数。
(2)下面是使用C语言描述这个算法的代码:
```c
#include <stdio.h>
void countCharacters(char* str, int* letters, int* spaces, int* digits, int* others) {
char ch;
int i = 0;
while (str[i]) {
ch = str[i];
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
(*lette