使用指针统计字符串中的数字字符的个数

《程序设计基础实训指导教程-c语言》
ISBN 978-7-03-032846-5
p92
5.1.2 上级实训内容

【实训内容6】统计字符串中的数字字符,如字符串中的内容是 2def35adh253kjsdf7/kj8655x,结果为11


分割线


isdigit函数

isdigit
语法:
  #include 
  int isdigit( int ch );
 
功能:如果参数是09之间的数字字符,函数返回非零值,否则返回零值.

    char c;
    scanf( "%c", &c );
    if( isdigit(c) )
    printf( "You entered the digit %c\n", c );

分割线


#include
#include
#define MAX 99
int main(void)
{
	char str[MAX];
	int i,cnt=0;
	puts("输入字符串:\n");
	gets(str);
	for(i=0;i<strlen(str);i++)
	{
		if(isdigit(*(str+i))!=0)
		{
			cnt++;
		}
	}
	printf("数字字符有:%d",cnt);
}

使用指针统计字符串中的数字字符的个数_第1张图片

你可能感兴趣的:(#,专升本c语言,c语言,指针)