Find Longest Word in String

Find Longest Word in String

Hard C++ Strings 32 views
Explanation Complexity

Problem Statement

Find and print the longest word from a sentence.
Real Life: Text analysis, finding important words.

Input Format

A full sentence (string with spaces).

Output Format

The longest word from the sentence.

Example

I love competitive programming very much
competitive

Constraints

• Sentence length ≥ 1

• Words are separated by spaces

• If multiple longest words exist, print the first one

Concept Explanation

We scan the sentence word by word and keep track of
which word has the maximum length.

Step-by-Step Explanation

1.Take the full sentence as input.

2.Initialize an empty string currentWord.

3.Initialize longestWord as empty.

4.Traverse the sentence character by character.

5.If character is not a space:

• Add it to currentWord.

6.If character is a space or end of sentence:

• Compare length of currentWord with longestWord.

• If currentWord is longer, update longestWord.

• Reset currentWord to empty.

7.After traversal, print longestWord.

Concept Explanation

We scan the sentence word by word and keep track of
which word has the maximum length.

Step-by-Step Explanation

1.Take the full sentence as input.

2.Initialize an empty string currentWord.

3.Initialize longestWord as empty.

4.Traverse the sentence character by character.

5.If character is not a space:

• Add it to currentWord.

6.If character is a space or end of sentence:

• Compare length of currentWord with longestWord.

• If currentWord is longer, update longestWord.

• Reset currentWord to empty.

7.After traversal, print longestWord.

Input / Output Format

Input Format
A full sentence (string with spaces).
Output Format
The longest word from the sentence.
Constraints
• Sentence length ≥ 1

• Words are separated by spaces

• If multiple longest words exist, print the first one

Examples

Input:
I love competitive programming very much
Output:
competitive

Example Solution (Public)

C++
void string_q11_longest_word() {
    string text = "Find the longest word in this sentence";
    string currentWord = "";
    string longestWord = "";
    
    for(int i = 0; i <= text.length(); i++) {
        if(i < text.length() && text[i] != ' ') {
            currentWord += text[i];
        } else {
            if(currentWord.length() > longestWord.length()) {
                longestWord = currentWord;
            }
            currentWord = "";
        }
    }
    
    cout << "Sentence: " << text << endl;
    cout << "Longest word: " << longestWord << endl;
    cout << "Length: " << longestWord.length() << endl;
}

Official Solution Code

void string_q11_longest_word() {
    string text = "Find the longest word in this sentence";
    string currentWord = "";
    string longestWord = "";
    
    for(int i = 0; i <= text.length(); i++) {
        if(i < text.length() && text[i] != ' ') {
            currentWord += text[i];
        } else {
            if(currentWord.length() > longestWord.length()) {
                longestWord = currentWord;
            }
            currentWord = "";
        }
    }
    
    cout << "Sentence: " << text << endl;
    cout << "Longest word: " << longestWord << endl;
    cout << "Length: " << longestWord.length() << endl;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.