File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Function to determine if a character is alphabetic
2
+
3
+ #include <stdio.h>
4
+ #include <stdbool.h>
5
+
6
+ bool alphabetic (const char c )
7
+ {
8
+ if ( (c >= 'a' && c <= 'z' ) || (c >= 'A' && c <= 'Z' ) )
9
+ return true;
10
+ else
11
+ return false;
12
+ }
13
+
14
+ /* Function to count the number of words in a string */
15
+
16
+ int countWords (const char string [])
17
+ {
18
+ int i , wordCount = 0 ;
19
+ bool lookingForWord = true, alphabetic (const char c );
20
+
21
+ for ( i = 0 ; string [i ] != '\0' ; ++ i )
22
+ if ( alphabetic (string [i ]) )
23
+ {
24
+ if ( lookingForWord )
25
+ {
26
+ ++ wordCount ;
27
+ lookingForWord = false;
28
+ }
29
+ }
30
+ else
31
+ lookingForWord = true;
32
+
33
+ return wordCount ;
34
+ }
35
+
36
+ int main (void )
37
+ {
38
+ const char text1 [] = "Well, here goes." ;
39
+ const char text2 [] = "And here we go... again." ;
40
+ int countWords (const char string []);
41
+
42
+ printf ("%s - words = %i\n" , text1 , countWords (text1 ));
43
+ printf ("%s - words = %i\n" , text2 , countWords (text2 ));
44
+
45
+ return 0 ;
46
+ }
You can’t perform that action at this time.
0 commit comments