Find Smallest and Largest Word of the String

Find Smallest and Largest Word of the String” is again a basic problem of string data structure. Here, we are given a string containing some words and our task is to find the largest and smallest word of the string.


Help Me Study Bro
Smallest Word: Me
Largest Word: Study

The steps required to find the smallest and largest word of the string are:

  1. Separate each word of the string (stringstream can be used in C++ to do the same task).
  2. Count each word length. Store the largest and smallest while scanning.
  3. Print smallest and largest word.

C++ Program to find the smallest and largest word of the string is as follows:

using namespace std;  
int main()  
    string str;  
    /* Scan the String */  
    cout<<"Enter a string:\n";  
    /* Word Variable to store word */  
    string word;  
    /* Making Stirng stream */  
    stringstream iss(str);   
    string smallest;  
    string largest;  
    int flag = 0;  
    /* Scanning each word of the string and 
       storing the result in temporary variables */  
    while (iss >> word)   
        if(flag == 0)  
            smallest = word;  
            largest = word;  
            flag = 1;  
            if(smallest.length() > word.length())  
            smallest = word;  
            else if(word.length() > largest.length())  
            largest = word;  
    /* Printing the Result */  
    cout<<"Smallest Word: "<<smallest;  
    cout<<"\nLargest Word: "<<largest;  

Enter a string: 
Help Me Study Bro
Smallest Word: Me
Largest Word: Study

Related Posts:

  1. Program to find Most Frequent Character of the String.
  2. Program to Remove all the blank spaces from the String.
  3. Program to check if String is isogram or not.
  4. Program to Reverse Each word of the String.
  5. Program to Print All the Substring of the Given String.
  6. Program to find longest palindromic Substring.
  7. Program to check Anagram Strings.
  8. Program to check whether two Strings are Rotation of each other or not.
  9. Program to check Palindromic Anagram.
  10. Program to print all the Palindromic Substring of the String.
  11. Program to check Panagram String.
  12. Program to find first non-repeating character of the String.
  13. Program to check Idempotent Matrix.
  14. Program to check Involuntary Matrix.
  15. Program to print matrix in zig-zag order.
  16. Program to print matrix in spiral order.
  17. Program to sort a matrix.
  18. Detect a Loop in a Linked List.
  19. Find the Length of the Loop present in the Linked List.
  20. Detect and Remove Loop from a Linked List.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *