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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Return maximum occurring character in an input string
- Print all the duplicates in the input string
- Remove characters from the first string which are present in the second string
- Remove duplicates from a given string
- A Program to check if strings are rotations of each other or not
- Print reverse of a string using recursion
- Write a program to print all permutations of a given string
- Divide a string in N equal parts
- Given a string, find its first non-repeating character
- Write a program to reverse an array or string
- Print list items containing all characters of a given word
- Reverse words in a given string
- Find the smallest window in a string containing all characters of another string
- Given a number, find the next smallest palindrome
- Lexicographic rank of a string
- An in-place algorithm for String Transformation
- Program to validate an IP address
- Count words in a given string
- String matching where one string contains wildcard characters
- Remove "b" and "ac" from a given string