用C语言输出一行字符串中的数字字符个数
时间: 2023-05-28 20:05:05 浏览: 68
#include <stdio.h>
int main()
{
char str[100];
int count = 0, i;
printf("输入一行字符串: ");
gets(str);
for(i=0; str[i]!=NULL; i++)
{
if(str[i]>='0' && str[i]<='9')
{
count++;
}
}
printf("字符串中的数字字符个数: %d",count);
return 0;
}
相关问题
用C语言输出一行字符串中的数字字符
#include <stdio.h>
int main()
{
char str[] = "Hello123World";
int i;
for (i=0; str[i]!='\0'; i++)
{
if (str[i] >= '0' && str[i] <= '9')
printf("%c", str[i]);
}
return 0;
}
c语言编写程序输出一行字符串中的数字字符。
可以使用C语言的isdigit()函数来判断每个字符是否是数字字符,然后输出即可。具体代码如下:
```
#include <stdio.h>
#include <ctype.h>
int main() {
char str[] = "hello123world456";
int len = strlen(str);
for (int i = 0; i < len; i++) {
if (isdigit(str[i])) {
printf("%c", str[i]);
}
}
return 0;
}
```
该程序会输出字符串中的数字字符,即123456。
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.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)