- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Modify a Sentence By Reversing Order Of Occurrences Of All Palindrome Words
Problem Statement
We have given a string str containing a total of N words. We need to find all palindrome words in the given string and create a new string by reversing the order of all palindrome words.
Sample Examples
Input
str = ‘nayan was gone to navjivan eye hospital’
Output
‘eye was gone to navjivan nayan hospital’
Explanation
The string contains three palindrome words: nayan, navjivan, and eye. We have reversed the order of all three words and kept all other words the same.
Input
‘Hello, users! How are you?’
Output
‘Hello, users! How are you?’
Explanation
It gives the same output as the string doesn’t contain any palindromic words.
Input
‘Your eye is beautiful.’
Output
‘Your eye is beautiful.’
Explanation
It gives the same output as a string containing only a single palindromic word.
Approach 1
In this approach, we will first split the string into words. After that, we will filter all palindromic words. Next, we reverse the order of all palindromic words.
At last, we iterate through the string, and if the current word is palindromic, we will replace it with another palindromic word in reverse order.
Algorithm
Step 1 − Execute the reversePlaindromic() function by passing a string as a parameter that returns the resultant string.
Step 2 − Create the isPalindrome() function, which checks whether the word is a palindrome.
Step 2.1 − Initialize the ‘start’ with 0 and ‘end’ with the string’s length – 1.
Step 2.2 − Use the while loop to iterate through the string, and compare the first and last character, compare the second and second last character, and so on. If any character doesn’t match, it returns false as it is not a palindromic string.
Step 2.3 − Return true if the string is palindromic.
Step 3 − Create a vector to store the words of the string. Also, define the ‘temp’ variable to store the word.
Step 4 − Use the for loop to iterate through the string, and append a character to the temp if it is not equal to space (‘ ’). Otherwise, push the value of temp to the allWords vector.
Step 5 − Iterate through the allWords vector and check if the current word is palindromic or not using the isPalindrome() function. If yes, push the word to the ‘palindromWords’ vector.
Step 6 − Reverse the ‘palindromWords’ list.
Step 7 − Now, again, iterate through the ‘allWords’ vector, and check if the current word is a palindrome. If yes, replace it with the respected word from the ‘palindromWords’ list.
Step 8 − Iterate through the ‘palindromWords’ list and create a string by appending all words to the result variable. Return the resultant string.
Example
#include <iostream> #include <vector> #include <algorithm> using namespace std; // Function to check if a string is a palindrome bool isPalindrome(string str){ int start = 0; int end = str.length() - 1; // iterate till start < end while (start < end){ // check if the character at the start and end are not the same and return false, else increment start and decrement end if (str[start] != str[end]){ return false; } else { start++; end--; } } return true; } string reversePalindromic(string str) { // vectors to store all words and palindromic words vector<string> palindromWords; vector<string> allWords; // variable to store single word string temp = ""; for (char x : str) { // If the current character is not space, then append it to temp; else, add temp to palindrome words and make temp NULL if (x != ' ') { temp += x; } else { allWords.push_back(temp); temp = ""; } } // push the last word to all words allWords.push_back(temp); // fetch all palindromic words for (string x : allWords){ if (isPalindrome(x)){ // Update newlist palindromWords.push_back(x); } } // Reverse the vector reverse(palindromWords.begin(), palindromWords.end()); int k = 0; for (int i = 0; i < allWords.size(); i++){ // If the current word is a palindrome, push it to palindrome words if (isPalindrome(allWords[i])){ allWords[i] = palindromWords[k]; k++; } } string result = ""; for (string x : allWords) { result += x; result += " "; } return result; } int main(){ string str = "nayan was gone to navjivan eye hospital"; string reverse = reversePalindromic(str); cout << reverse << endl; return 0; }
Output
eye was gone to navjivan nayan hospital
Time complexity − O(N), as we are iterating through the string of length N.
Space complexity − O(K), as we use the list to store words, where k is the total number of words in the string.
Conclusion
We learned to get all palindrome words from sentences and add them in the reverse order. In the above code, coders can try to change the implementation of the isPalindrome() function to learn something new.