c语言题目内容:输入一个字符串,存在字符数组中,输出该字符串中字母和数字的个数。 输入格式: 字符串(最长为20个字符)。 输出格式:空格隔开的两个整数,第一个表示字母个数,第二个表示数字个数。
时间: 2023-06-10 19:03:13 浏览: 84
好的,我明白了。对于你的问题,可以使用C语言中的字符处理函数和循环结构来解决。下面是一份示例代码:
```c
#include <stdio.h>
#include <ctype.h>
#define MAX_LEN 20
int main() {
char str[MAX_LEN + 1];
int i, letterCount = 0, digitCount = 0;
printf("请输入一个字符串(最长为20个字符):");
fgets(str, MAX_LEN + 1, stdin);
for (i = 0; str[i] != '\0' && str[i] != '\n'; i++) {
if (isalpha(str[i])) {
letterCount++;
} else if (isdigit(str[i])) {
digitCount++;
}
}
printf("字母个数:%d,数字个数:%d", letterCount, digitCount);
return 0;
}
```
以上代码使用了fgets()函数来读入字符串,并使用isalpha()和isdigit()函数来判断字符是否为字母和数字。循环遍历输入的字符串,统计字母和数字的个数。最后输出结果。
相关问题
c语言用数组输入一个字符串,统计字符串中字母、数字和其它字符的个数。
可以使用字符数组来存储字符串,然后遍历数组,统计字母、数字和其它字符的个数。代码如下:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int letterCnt = 0, digitCnt = 0, otherCnt = 0;
printf("请输入一个字符串:");
gets(str);
for (int i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) {
letterCnt++;
} else if (isdigit(str[i])) {
digitCnt++;
} else {
otherCnt++;
}
}
printf("字母个数:%d\n数字个数:%d\n其它字符个数:%d\n", letterCnt, digitCnt, otherCnt);
return 0;
}
```
其中,`isalpha()` 和 `isdigit()` 是 C 标准库中的函数,用于判断字符是否为字母和数字。`gets()` 函数用于从标准输入中读取字符串。注意,`gets()` 函数存在安全问题,建议使用 `fgets()` 函数替代。
c语言实现:输入一个字符串,统计英语字母、数字字符、其它字符的个数。
以下是C语言实现,可以使用`ctype.h`头文件中的函数判断字符类型:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int letter = 0, digit = 0, other = 0;
int i;
printf("请输入一个字符串:");
fgets(str, 100, stdin);
for (i = 0; str[i] != '\0'; i++) {
if (isalpha(str[i])) { // 判断是否为英文字母
letter++;
} else if (isdigit(str[i])) { // 判断是否为数字字符
digit++;
} else { // 其它字符
other++;
}
}
printf("字母个数:%d\n", letter);
printf("数字个数:%d\n", digit);
printf("其它字符个数:%d\n", other);
return 0;
}
```
以上代码中,`fgets()`函数用于从标准输入中获取字符串,`isalpha()`函数判断字符是否为英文字母,`isdigit()`函数判断字符是否为数字字符。
相关推荐
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)