-
Notifications
You must be signed in to change notification settings - Fork 0
/
49_character_array.cpp
64 lines (61 loc) · 1.13 KB
/
49_character_array.cpp
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
//49. largest word length in a sentence
#include<bits/stdc++.h>
using namespace std;
int main()
{
char arr[] = "she has more money than he";
int maxi = INT_MIN,cnt=0;
for(int i =0; arr[i]; i++)
{
if(arr[i] != ' ')
{
cnt++;
}
else
{
maxi = max(maxi,cnt);
cnt=0;
}
}
maxi = max(maxi,cnt);
cout<<maxi;
}
//largest word length with word in a sentence
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i=0;
cin>>n;
cin.ignore();
char arr[n+1];
cin.getline(arr,n);
cin.ignore();
int curlen = 0, maxlen = 0;
int curst =0 , maxst = 0;
while(1)
{
if(arr[i] == ' ' || arr[i] == '\0')
{
if(curlen > maxlen)
{
maxlen = curlen;
maxst = curst;
}
curlen =0;
curst = i+1;
}
else
curlen++;
if(arr[i] == '\0')
{
break;
}
i++;
}
cout<<maxlen<<endl;
for(int i =0 ;i<maxlen;i++)
{
cout<<arr[i+maxst];
}
}