用C语言实现:输入一个字符串,统计其中数字字符的个数。(10分) 程序运行结果输出示例 请输入一个字符串: abc123def456gh 字符串中数字字符的个数为: 6
时间: 2024-05-10 21:14:59 浏览: 21
以下是用C语言实现的程序:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int count = 0;
printf("请输入一个字符串: ");
scanf("%s", str);
for (int i = 0; str[i] != '\0'; i++) {
if (isdigit(str[i])) {
count++;
}
}
printf("字符串中数字字符的个数为: %d\n", count);
return 0;
}
```
程序运行结果输出示例:
```
请输入一个字符串: abc123def456gh
字符串中数字字符的个数为: 6
```
相关问题
用C语言编写一个程序:键盘输入一个字符串,统计其中数字、英文字符、空格和其它字符的个数。
我可以回答这个问题。下面是用C语言编写的示例代码:
```c
#include <stdio.h>
int main() {
char str[100];
int i, digits = 0, letters = 0, spaces = 0, others = 0;
printf("请输入一串字符串:");
gets(str);
for (i = 0; str[i] != '\0'; i++) {
if (str[i] >= '0' && str[i] <= '9') {
digits++;
} else if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')) {
letters++;
} else if (str[i] == ' ') {
spaces++;
} else {
others++;
}
}
printf("字符串中有 %d 个数字,%d 个英文字符,%d 个空格,%d 个其他字符。\n", digits, letters, spaces, others);
return 0;
}
```
它会读取一个字符串,并统计其中数字、英文字符、空格和其他字符的个数。
c语言编写程序输入一个字符串,统计字符串中字符的个数
可以使用C语言的字符串库函数`strlen()`来统计字符串中字符的个数。以下是一个示例程序:
```c
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int len;
printf("请输入一个字符串:");
scanf("%s", str);
len = strlen(str);
printf("该字符串中字符的个数为:%d\n", len);
return 0;
}
```
在程序中,先定义一个字符数组`str`,用于存储输入的字符串。然后使用`scanf()`函数获取用户输入的字符串。接着调用`strlen()`函数获取字符串中字符的个数,并将结果赋给变量`len`。最后,使用`printf()`函数输出结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.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)