forked from portfoliocourses/c-example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_digits.c
40 lines (34 loc) · 1011 Bytes
/
count_digits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*******************************************************************************
*
* Program: Standar deviaton demonstration
*
* Description: Example of calculating standard deviation in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=epqWMKa--xk
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count_digits(char *s);
int main()
{
// test the function
char s[] = "asdfasdfsdaHFGGJGJG 123456789 @#$@#^$%^$#%^";
int result = count_digits(s);
printf("digit total: %d\n", result);
return 0;
}
// returns the number of digits in string s
int count_digits(char *s)
{
int length = strlen(s);
int count = 0;
// examine each character in the string using isdigit which returns true
// if the character is a digit... and keep a running count
for (int i = 0; i < length; i++)
if (isdigit(s[i])) count++;
return count;
}