-
Notifications
You must be signed in to change notification settings - Fork 121
/
count_punct.c
40 lines (34 loc) · 984 Bytes
/
count_punct.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: Punctuation marks counter demonstration
*
* Description: Example of counting the punctuation marks in a string in C.
*
* YouTube Lesson: https://www.youtube.com/watch?v=7SRd_gZDqCE
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count_punct(char *s);
int main()
{
// test the function
char s[] = "apple, pear, orange; something...";
int total = count_punct(s);
printf("total: %d\n", total);
return 0;
}
// returns the number of punctuation marks in the string param s
int count_punct(char *s)
{
int len = strlen(s);
int count = 0;
// examine every char, ispunct lets us know if it is a punctuation mark,
// keep a running count
for (int i = 0; i < len; i++)
if (ispunct(s[i])) count++;
return count;
}