1. 程式人生 > >使用函式統計指定數字的個數 (10 分)

使用函式統計指定數字的個數 (10 分)

#include <stdio.h>

int CountDigit( int number, int digit );

int main()
{
    int number, digit;

    scanf("%d %d", &number, &digit);
    printf("Number of digit %d in %d: %d\n", digit, number, CountDigit(number, digit));

    return 0;
}

int CountDigit(int number,int digit)

{
//	int number,digit,count;
	int count=0;
	int d[10];
	int z=0;
//	printf("Enter a number:");
//	scanf("%d",&number);
	if(number<0)
	number=-number;
	do{
		d[z++] = number%10;
		number=number/10;
//		count++;
//		printf("%d\n",d);
		
	}while(number!=0);
	for(int i=0;i<z;i++){
//		printf("%d\n",d[i]);
		if(digit == d[i]){
			count++;
		}
	}
	
	return count;
}