Program to find Smallest and Largest Word in a String

Given a string, find the minimum and the maximum length words in it.
Examples:

Input : "This is a test string"
Output : Minimum length word: is
         Maximum length word: string

Input : "GeeksforGeeks A computer Science portal for Geeks"
Output : Minimum length word: A
         Maximum length word: GeeksforGeeks

Approach

The idea is to keep a starting index si and an ending index ei.



  • si points to the starting of a new word and we traverse the string using ei.
  • Whenever a space or ‘\0’ character is encountered,we compute the length of the current word using (ei – si) and compare it with the minimum and the maximum length so far.
    • If it is less, update the min_length and the min_start_index( which points to the starting of the minimum length word).
    • If it is greater, update the max_length and the max_start_index( which points to the starting of the maximum length word).
  • Finally update minWord and maxWord which are output strings that have been sent by reference with the substrings starting at min_start_index and max_start_index of length min_length and max_length respectively.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find Smallest and 
// Largest Word in a String
#include<iostream>
#include<cstring>
using namespace std;
  
void minMaxLengthWords(string input, string &minWord, string &maxWord) 
{
    // minWord and maxWord are received by reference 
    // and not by value
    // will be used to store and return output
    int len = input.length();
    int si = 0, ei = 0;
    int min_length = len, min_start_index = 0, max_length = 0, max_start_index = 0;
  
    // Loop while input string is not empty
    while (ei <= len) 
    {
        if (ei < len && input[ei] != ' ')
            ei++;
          
        else 
        {
            // end of a word
            // find curr word length
            int curr_length = ei - si;
          
            if (curr_length < min_length) 
            {
                min_length = curr_length;
                min_start_index = si;
            }
              
            if (curr_length > max_length)
            {
                max_length = curr_length;
                max_start_index = si;
            }
            ei++;
            si = ei;
        }
    }
      
    // store minimum and maximum length words
    minWord = input.substr(min_start_index, min_length);
    maxWord = input.substr(max_start_index, max_length); 
}
  
// Driver code
int main() 
{
    string a = "GeeksforGeeks A Computer Science portal for Geeks";
    string minWord, maxWord;
    minMaxLengthWords(a, minWord, maxWord);
      
    // to take input in string use getline(cin, a);
    cout << "Minimum length word: "
        << minWord << endl
        << "Maximum length word: "
        << maxWord << endl;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to find Smallest and 
// Largest Word in a String
class GFG
{
  
    static String minWord = "", maxWord = "";
  
    static void minMaxLengthWords(String input) 
    {
        // minWord and maxWord are received by reference 
        // and not by value
        // will be used to store and return output
        int len = input.length();
        int si = 0, ei = 0;
        int min_length = len, min_start_index = 0,
              max_length = 0, max_start_index = 0;
  
        // Loop while input string is not empty
        while (ei <= len) 
        {
            if (ei < len && input.charAt(ei) != ' ')
            {
                ei++;
            
            else
            {
                // end of a word
                // find curr word length
                int curr_length = ei - si;
  
                if (curr_length < min_length) 
                {
                    min_length = curr_length;
                    min_start_index = si;
                }
  
                if (curr_length > max_length) 
                {
                    max_length = curr_length;
                    max_start_index = si;
                }
                ei++;
                si = ei;
            }
        }
  
        // store minimum and maximum length words
        minWord = input.substring(min_start_index, min_start_index + min_length);
        maxWord = input.substring(max_start_index, max_length);
    }
  
    // Driver code
    public static void main(String[] args)
    {
        String a = "GeeksforGeeks A Computer Science portal for Geeks";
  
        minMaxLengthWords(a);
  
        // to take input in string use getline(cin, a);
        System.out.print("Minimum length word: "
                + minWord
                + "\nMaximum length word: "
                + maxWord);
    }
}
  
// This code contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to find Smallest and 
// Largest Word in a String
using System;
  
class GFG
{
  
    static String minWord = "", maxWord = "";
  
    static void minMaxLengthWords(String input) 
    {
        // minWord and maxWord are received by reference 
        // and not by value
        // will be used to store and return output
        int len = input.Length;
        int si = 0, ei = 0;
        int min_length = len, min_start_index = 0,
            max_length = 0, max_start_index = 0;
  
        // Loop while input string is not empty
        while (ei <= len) 
        {
            if (ei < len && input[ei] != ' ')
            {
                ei++;
            
            else
            {
                // end of a word
                // find curr word length
                int curr_length = ei - si;
  
                if (curr_length < min_length) 
                {
                    min_length = curr_length;
                    min_start_index = si;
                }
  
                if (curr_length > max_length) 
                {
                    max_length = curr_length;
                    max_start_index = si;
                }
                ei++;
                si = ei;
            }
        }
  
        // store minimum and maximum length words
        minWord = input.Substring(min_start_index, min_length);
        maxWord = input.Substring(max_start_index, max_length);
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        String a = "GeeksforGeeks A Computer Science portal for Geeks";
  
        minMaxLengthWords(a);
  
        // to take input in string use getline(cin, a);
        Console.Write("Minimum length word: "
                + minWord
                + "\nMaximum length word: "
                + maxWord);
    }
}
  
// This code has been contributed by 29AjayKumar

chevron_right


Output:

Minimum length word: A
Maximum length word: GeeksforGeeks

This article is contributed by Aditi Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Rajput-Ji, 29AjayKumar



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.