
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Rearrange Words in a Sentence in C++
Suppose we have a string with different words, that string is called sentence, and this is in the following format −
First letter is in upper case.
Each word in text are separated by a single space character.
We have to rearrange the words in text such that all words are rearranged in increasing order of their lengths. If two words have the same length, arrange them in their original order.
Then finally return the string by applying these rules.
So, if the input is like "I love to code in cpp", then the output will be "I to in cpp love code"
To solve this, we will follow these steps −
make the first character of text into small letter
Define an array x := put all words after splitting text using space
Define an array s of pairs
for initialize i := 0, when i < size of x, update (increase i by 1), do −
insert {x[i],i} at the end of s
sort the array s based on length, if lengths are same use index values
ret := empty string
for initialize i := 0, when i < size of s, update (increase i by 1), do −
ret := ret concatenate first element of s[i]
if i is not equal to size of s, then −
ret := ret concatenate with blank space
make first character of ret to capital
return ret
Example
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: vector <string> split(string& s, char delimiter){ vector <string> tokens; string token; istringstream tokenStream(s); while(getline(tokenStream, token, delimiter)){ tokens.push_back(token); } return tokens; } static bool cmp(pair <string, int>& a, pair <string, int>& b){ if(a.first.size() != b.first.size()) return a.first.size() < b.first.size(); return a.second < b.second; } static bool a(string& a, string& b){ return a.size() < b.size(); } string arrangeWords(string text) { text[0] += 'a' - 'A'; vector<string> x = split(text, ' '); vector<pair<string, int> > s; for (int i = 0; i < x.size(); i++) s.push_back({ x[i], i }); sort(s.begin(), s.end(), cmp); string ret = ""; for (int i = 0; i < s.size(); i++) { ret += s[i].first; if (i != s.size() - 1) ret += ' '; } ret[0] += 'A' - 'a'; return ret; } }; main(){ Solution ob; cout << (ob.arrangeWords("I love to code in cpp")); }
Input
"I love to code in cpp"
Output
I to in cpp love code
- Related Articles
- Count words in a sentence in Python program
- Count palindrome words in a sentence in C++
- Python program to count words in a sentence
- Counting number of words in a sentence in JavaScript
- Python program to sort Palindrome Words in a Sentence
- Arranging words by their length in a sentence in JavaScript
- Split the sentence into words in C++
- Program to rearrange spaces between words in Python
- Finding n most frequent words from a sentence in JavaScript
- Print all words occurring in a sentence exactly K times
- Python - Generate all possible permutations of words in a Sentence
- Replace all occurrence of specific words in a sentence based on an array of words in JavaScript
- Program to reverse a sentence words stored as character array in C++
- Python - Check if given words appear together in a list of sentence
- Java program to sort words of sentence in ascending order
