w3resource

C++ String Exercises: Find the largest word in a given string

C++ String: Exercise-4 with Solution

Write a C++ program to find the largest word in a given string.

Visual Presentation:

C++ Exercises: Find the largest word in a given string

Sample Solution:

C++ Code :

#include <iostream> // Including input/output stream library
#include <string>   // Including string library for string manipulation

using namespace std; // Using the standard namespace

// Function to find the longest word in a string
string Longest_Word(string text) {

    string result_word, temp_str1; // Declare strings to store result and temporary substrings

    // Loop through each character in the string
    for (int x = 0; x < text.length(); x++)
    {
        // Check if the character is alphanumeric (letter or number)
        if (text[x] != ' ' && (int(text[x]) >= 65 && int(text[x]) <= 90) || (int(text[x]) >= 97 && int(text[x]) <= 122) || (int(text[x] >= 48 && int(text[x]) <= 57)))
        {
            result_word.push_back(text[x]); // Add the character to the result word
        }
        else
        {
            break; // Break the loop if it's not alphanumeric
        }
    }

    // Loop through each character in the string
    for (int x = 0; x < text.length(); x++)
    {
        // Check if the character is alphanumeric
        if (text[x] != ' ' && (int(text[x]) >= 65 && int(text[x]) <= 90) || (int(text[x]) >= 97 && int(text[x]) <= 122) || (int(text[x] >= 48 && int(text[x]) <= 57)))
        {
            temp_str1.push_back(text[x]); // Add the character to the temporary substring

            // Check if it's the last character of the string and the temp substring is longer than the result word
            if (x + 1 == text.length() && temp_str1.length() > result_word.length())
            {
                result_word = temp_str1; // Update the result word
            }
        }
        else
        {
            // Check if the temporary substring is longer than the result word
            if (temp_str1.length() > result_word.length())
            {
                result_word = temp_str1; // Update the result word
            }

            temp_str1.clear(); // Clear the temporary substring
        }
    }

    return result_word; // Return the longest word found in the string
}

int main() {
    // Display the original string and its longest word
    cout << "Original String: C++ is a general-purpose programming language. \nLongest word: " << Longest_Word("C++ is a general-purpose programming language.") << endl;

    // Display another original string and its longest word
    cout << "\nOriginal String: The best way we learn anything is by practice and exercise questions. \nLongest word: " << Longest_Word("The best way we learn anything is by practice and exercise questions.") << endl;

    // Display another original string and its longest word
    cout << "\nOriginal String: Hello \nLongest word: " << Longest_Word("Hello") << endl;

    return 0; // Return 0 to indicate successful completion
}

Sample Output:

Original String: C++ is a general-purpose programming language. 
Longest word: programming

Original String: The best way we learn anything is by practice and exercise questions. 
Longest word: questions

Original String: Hello 
Longest word: Hello

Flowchart:

Flowchart: Find the largest word in a given string.

C++ Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Capitalize the first letter of each word of a string.
Next C++ Exercise: Sort characters in a string.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.