C++ 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.
Pictorial Presentation:
Sample Solution:
C++ Code :
#include <iostream>
#include <string>
using namespace std;
string Longest_Word(string text) {
string result_word, temp_str1;
for (int x = 0; x < text.length(); x++)
{
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]);
}
else
{
break;
}
}
for (int x = 0; x < text.length(); x++)
{
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]);
if (x + 1 == text.length() && temp_str1.length() > result_word.length())
{
result_word = temp_str1;
}
}
else
{
if (temp_str1.length() > result_word.length())
{
result_word = temp_str1;
}
temp_str1.clear();
}
}
return result_word;
}
int main() {
cout << "Original String: C++ is a general-purpose programming language. \nLongest word: " << Longest_Word("C++ is a general-purpose programming language.") << endl;
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;
cout << "\nOriginal String: Hello \nLongest word: " << Longest_Word("Hello") << endl;
return 0;
}
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
Flowchart:
C++ Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a C++ program to capitalize the first letter of each word of a given string. Words must be separated by only one space.
Next: Write a C++ program to sort characters (numbers and punctuation symbols are not included) in a string.
What is the difficulty level of this exercise?