-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrint the number of words of a sentence.c
61 lines (52 loc) · 1.2 KB
/
Print the number of words of a sentence.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
/*****
@author: Nilay Chandra Barman (https://github.com/Nilaycb)
Copyright (c) 2020, All rights reserved.
Copyrights licensed under the GNU GPLv3 License.
******/
#include<stdio.h>
#include<conio.h>
int main()
{
char st[100];
int i=0, cnt=0, p;
int sp_flag=0; //flag to identify whether a space/split was previously found or not
printf("## A program to print the number of words of a sentence ##\n\n");
printf("Enter a sentence: ");
while(1)
{
scanf("%c", &st[i]);
if(st[i] == '\n')
{
break;
}
i++;
}
printf("\nThe input sentence: ");
for(p=0; p<i; p++)
{
printf("%c", st[p]);
if((p == 0) && (st[p] == ' '))
{
sp_flag = 1;
}
else if((st[p] == ' '))
{
if(sp_flag == 0)
{
cnt++;
sp_flag = 1; //space found
}
}
else
{
sp_flag = 0; //char found and space flag reset
//char found and loop ends
if(p == i-1)
{
cnt++;
}
}
}
printf("\n\nTotal words found: %d\n", cnt);
return 0;
}