-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path11_count_words.c
73 lines (54 loc) · 1.53 KB
/
11_count_words.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// // Write a function to count words in a given string.
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define ARRAY_SIZE 31
// // Function Declarations
int strLength(char[]);
int countWords(char[]);
// // Main Function Start
int main()
{
char str[ARRAY_SIZE];
printf("\nEnter Any String to Count Words in It (MAX CHARACTERS %d) => ", ARRAY_SIZE - 1);
fgets(str, ARRAY_SIZE, stdin); // // Input String
str[strcspn(str, "\n")] = '\0'; // // Replace '\n' character with '\0' in str
int totalWords = countWords(str);
printf("\nThere Are %d Words in \"%s\"", totalWords, str);
putch('\n');
getch();
return 0;
}
// // Main Function End
// // Function Definitions 👇👇
// // Function to Calculate Length of String
int strLength(char str[])
{
int length = 0;
while (str[length])
length++;
return length;
}
// // Function to Count words in a Given String
int countWords(char str[])
{
// // ASCII Code of Space ' ' is 32
// // ASCII Code of Horizontal Tab '\t' is 9
// // Skip Leading Spaces in String
int j = 0;
while (str[j] == 32 || str[j] == '\t')
j++;
if (str[j] == '\0')
return 0; // // No words in string
int count = 1; // // Counter to count words
// // Count words
for (int i = j; str[i]; i++)
{
if (str[i + 1] != '\0' && str[i] == 32 && str[i + 1] != 32 && str[i] == '\t' && str[i + 1] != '\t')
{
count++;
}
}
return count; // // return total words
}