Given a string, find the minimum and the maximum length words in it.
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
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.
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 firstname.lastname@example.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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Program to find the largest and smallest ASCII valued characters in a string
- Find largest word in dictionary by deleting some characters of given string
- C program to find and replace a word in a File by another given word
- Find the first repeated word in a string
- Program to find the Encrypted word
- Find the first maximum length even word from a string
- Find frequency of each word in a string in Python
- Find the smallest window in a string containing all characters of another string
- Find Kth largest string from the permutations of the string with two characters
- C program to Replace a word in a text by another given word
- Find lexicographically smallest string in at most one swaps
- Find the lexicographically smallest string which satisfies the given condition
- Find the largest Alphabetic character present in the string
- Find the lexicographically largest palindromic Subsequence of a String
- Most frequent word in first String which is not present in second String