Skip to content

Commit d0fefda

Browse files
committed
count words in text
1 parent 38ed9eb commit d0fefda

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

prog.10.8.c

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
4+
bool alphabetic (const char c)
5+
{
6+
if ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
7+
return true;
8+
else
9+
return false;
10+
}
11+
12+
void readLine (char buffer[])
13+
{
14+
char character;
15+
int i = 0;
16+
17+
do
18+
{
19+
character = getchar ();
20+
buffer[i] = character;
21+
++i;
22+
}
23+
while ( character != '\n' );
24+
25+
buffer[i - 1] = '\0';
26+
}
27+
28+
int countWords (const char string[])
29+
{
30+
int i, wordCount = 0;
31+
bool lookingForWord = true, alphabetic (const char c);
32+
33+
for ( i = 0; string[i] != '\0'; ++i )
34+
if ( alphabetic(string[i]) )
35+
{
36+
if ( lookingForWord )
37+
{
38+
++wordCount;
39+
lookingForWord = false;
40+
}
41+
}
42+
else
43+
lookingForWord = true;
44+
45+
return wordCount;
46+
}
47+
48+
int main (void)
49+
{
50+
char text[81];
51+
int totalWords = 0;
52+
int countWords (const char string[]);
53+
void readLine (char buffer[]);
54+
bool endOfText = false;
55+
56+
printf ("Type in your text.\n");
57+
printf ("When you are done, press 'RETURN'.\n\n");
58+
59+
while ( ! endOfText )
60+
{
61+
readLine (text);
62+
63+
if ( text[0] == '\0' )
64+
endOfText = true;
65+
else
66+
totalWords += countWords (text);
67+
}
68+
69+
printf ("\nThere are %i words in the above text.\n", totalWords);
70+
71+
return 0;
72+
}

0 commit comments

Comments
 (0)