forked from randerson112358/C-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountWords.c
More file actions
40 lines (31 loc) · 1.11 KB
/
Copy pathcountWords.c
File metadata and controls
40 lines (31 loc) · 1.11 KB
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
/*
This program takes a string (sentence or text) as input
and count the number of words in the input string
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char words[200];// text that can hold 200 characters
int i, count = 0;
printf("Enter the sentence or text:\n");
fgets(words, sizeof(words),stdin); // Scan in the full text
//Check if the first character is alphabetic
if(isalpha(words[0]))
count = 1;
//Loop through the full text until the user hits enter which is the '\0' character
for (i = 0;words[i] != '\0';i++)
{
//if the text character is a space a.k.a ' ',
//then the characters before it must've formed a word
if(words[i]== ' '){
//Loop through all other spaces if we encounter more than 1
while(isspace(words[i])){i++;}
//If the next character isn't the enter key then there are more words to count
if(words[i] != '\0')
count++;
}
}
printf("The number of words in given sentence/text are: %d\n", count);
}