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 email@example.com. 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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- C program to find and replace a word in a File by another given word
- C program to Replace a word in a text by another given word
- Find the word from a given sentence having given word as prefix
- Find largest word in dictionary by deleting some characters of given string
- Program to find the largest and smallest ASCII valued characters in a string
- Longest Common Prefix using Word by Word Matching
- Lexicographically smallest and largest anagrams of a string containing another string as its substring
- String containing first letter of every word in a given string with spaces
- Most frequent word in first String which is not present in second String
- Program to find the Encrypted word
- Find the first repeated word in a string
- Find frequency of each word in a string in Python
- Find the first maximum length even word from a string
- Print the first and last character of each word in a String
- Remove the first and last character of each word in a string
- Capitalize the first and last character of each word in a string
- Reverse every word of the string except the first and the last character
- Lexicographically smallest and largest substring of size k
- Largest and Smallest N-digit Octal Numbers
- Find Kth largest string from the permutations of the string with two characters