Skip to content

Commit 38ed9eb

Browse files
committed
word count
1 parent 58db924 commit 38ed9eb

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

prog.10.7.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

0 commit comments

Comments
 (0)