 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find Smallest and Largest Word in a String in C++
In this problem, we are given string str. Our task is to create a program to find Smallest and Largest Word in a String in C++.
Problem Description − Here, we have a string we need to find the word whose length is maximum and minimum out of all words in the string. The word is separated using white spaces or null(\0) characters.
Let’s take an example to understand the problem
Input
str = “Learn Programming at TutorialsPoint”
Output
smallest word = at largest word = Tutorialspoint
Solution Approach
To find the smallest and largest word, we will find the length of each word by using two indexes, one for the start of the word and one for the ending which is marked using the ‘ ’ (space character) or ‘\0’ character. Then using the start and end index, we will find the maxLength and minlength. And update the smallestWord and largestWord based on the current word’s length.
Program to illustrate the working of our solution
Example
#include<iostream> #include<cstring> using namespace std; void minMaxLengthWords(string str){    int StrLength = str.length();    int startIndex = 0, endIndex = 0;    int minLength = StrLength, maxLength = 0, currentLength;    string smallest, largest;    while (endIndex <= StrLength){       if (str[endIndex] != '\0' && str[endIndex] != ' ')          endIndex++;       else{          currentLength = endIndex - startIndex;          if (currentLength < minLength){             smallest = str.substr(startIndex, currentLength);             minLength = currentLength;          }          if (currentLength > maxLength){             largest = str.substr(startIndex, currentLength);             maxLength = currentLength;          }          endIndex++;          startIndex = endIndex;       }    }    cout<<"Smallest Word from the string is "<<smallest<<"\n";    cout<<"Smallest Word from the string is "<<largest; } int main() {    string a = "Learn Programming at TutorialsPoint";    minMaxLengthWords(a); } Output
Smallest Word from the string is at Smallest Word from the string is TutorialsPoint
