-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrint the biggest word with size of a sentence.c
121 lines (103 loc) · 2.66 KB
/
Print the biggest word with size 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*****
@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], tmp_st[100], big_st[100];
int i=0, p, res=1, j=0, max=0, m=0, v=0;
printf("## A program to find the biggest word with size 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]);
}
printf("\n\nThe input sentence length: %d\n", i);
res=1; //whether a new word starts or not
m=0; //index and length for new tmp_st[] array
v=0; //index for new big_st[] array
for(p=0; p<i; p++)
{
if(res == 1)
{
j=0;
if(st[p] != ' ')
{
j++; //j=1 also applicable
res=0;
tmp_st[m] = st[p];
m++;
//[important] need to set the max when the loop ends by default at the start
if(j>max)
{
max = j;
for(v=0; v<m; v++)
{
big_st[v] = tmp_st[v];
}
}
}
}
else
{
if(st[p] == ' ')
{
res = 1;
if(j>max)
{
max = j;
for(v=0; v<m; v++)
{
big_st[v] = tmp_st[v];
}
}
else
{
//new word is not bigger
//need to drop the letters from tmp_st[] array
while(m--)
{
tmp_st[m] = '\0';
}
m=0;
}
}
else
{
j++;
tmp_st[m] = st[p];
m++;
//[important] need to set the max when the loop ends by default at the end
if(j>max)
{
max = j;
for(v=0; v<m; v++)
{
big_st[v] = tmp_st[v];
}
}
}
}
}
printf("\nThe biggest word size is: %d\n", max);
printf("\nThe biggest word is: ");
for(p=0; p<max; p++)
{
printf("%c", big_st[p]);
}
printf("\n");
return 0;
}