-
Notifications
You must be signed in to change notification settings - Fork 0
/
program9-7.c
50 lines (40 loc) · 1.03 KB
/
program9-7.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
41
42
43
44
45
46
47
48
49
50
// Counting Words
// Function to determine if a character is alphabetic
#include <stdio.h>
#include <stdbool.h>
bool alphabetic (const char c)
{
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
return true;
else
return false;
}
/* Function to count the number of
words in a string */
int countWords (const char string[])
{
int i, wordCount = 0;
bool lookingForWord = true,
alphabetic (const char c);
for (i = 0; string[i] != '\0'; ++i)
if (alphabetic(string[i]))
{
if (lookingForWord)
{
++wordCount;
lookingForWord = false;
}
}
else
lookingForWord = true;
return wordCount;
}
int main (void)
{
const char text1[] = "Well, here goes.";
const char text2[] = "And here we go... again.";
int countWords (const char string[]);
printf ("%s - words = %i\n", text1, countWords (text1));
printf ("%s - words = %i\n", text2, countWords (text2));
return 0;
}