-
Notifications
You must be signed in to change notification settings - Fork 492
/
LongestWordInSentence.cpp
61 lines (50 loc) Β· 1.76 KB
/
LongestWordInSentence.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
/*
Given a string, we have to find the longest word in the input string and then display that word also the length of that word.
Eg:
Input : A computer science portal for geeks
Output : The longest word in a sentence is computer having length 8
*/
#include <iostream>
#include <string>
using namespace std;
string longest_word(string s)
{
int len = s.length();
// we are taking four pointers starting for denoting start & end for denoting end and maxlen & max_starting for taking account of starting of max_length word with its length.
int starting = 0, end = 0, max_len = 0, max_starting = 0;
// Loop till full string is not traversed
while (end <= len)
{
if (end < len and s[end] != ' ') // If char is not space then increase the end pointer
{
end ;
}
else
{
int curr_length = end - starting; // Finding the length of the word that is traversed
if (curr_length > max_len) // If word length is greater than the max length that stored then change max_len and starting of max len word.
{
max_len = curr_length;
max_starting = starting;
}
end ;
starting = end;
}
}
return s.substr(max_starting, max_len); // Simply return that longest word or string in the sentence.
}
int main()
{
ios_base::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
string s;
getline(cin, s); // Getting Line as a input.
string ans = longest_word(s);
cout << "The longest word in a sentence is " << ans << " having length " << ans.length();
return 0;
}
/*
Input :- This is sample code based on string for GSSOC'22
Output:- The longest word in a sentence is GSSOC'22 having length 8
*/