How to count the digits in string with C

1 Answer

0 votes
#include <stdio.h>
#include <ctype.h>

int count_digits(char *s) {
	int count = 0;
	for (int i = 0; s[i] != '\0'; i++) {
		if (isdigit(s[i])) {
			count++;
		}
    }
   
   return count;
}

int main(void) {
	char s[] = "1, 7, 9, 12, 899";

	printf("%d\n", count_digits(s));
   
    return 0;
}


     
/*
run:
      
8
 
*/

 



answered Jul 2, 2020 by avibootz

Related questions

1 answer 142 views
1 answer 104 views
1 answer 116 views
1 answer 133 views
3 answers 278 views
278 views asked Oct 1, 2014 by avibootz
1 answer 101 views
1 answer 99 views
99 views asked Jul 21, 2024 by avibootz
...