-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcountWordsInLine.c
47 lines (37 loc) · 1006 Bytes
/
countWordsInLine.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
/*
* Program to count no. of words in a line
*/
#include <stdio.h>
#include <string.h>
int main () {
/*
* Solution approach:
* Step 1: Declare a character array,
* Take the string as input,
* Initialize an integer countOfWords with 0
* Step 2: Iterate over all the characters of the string
* If you get a space, or string has terminated ('\0')
* simply increment the countOfWords
* Step 3:
* Print the variable countOfWords
*/
char line[100];
// line points to the first character in the string
// &line doesn't make any sense
// in scanf, we give the address where the variable is stored
// scanf("%s", &line); => INCORRECT
// scanf("%s", line);
gets(line);
// printf("Line: %s", line);
int countOfWords = 0;
int lengthOfLine = strlen(line);
for (int i = 0; i < lengthOfLine; i ++) {
if (line[i] == ' ') {
countOfWords ++;
}
}
// take care of the last word
countOfWords ++;
printf("Count of words in line is: %d\n", countOfWords);
return 0;
}