Home  >  Article  >  Backend Development  >  Modify the sentence by reversing the order in which all palindromic words appear

Modify the sentence by reversing the order in which all palindromic words appear

WBOY
WBOYforward
2023-08-27 10:01:12638browse

Modify the sentence by reversing the order in which all palindromic words appear

Problem Statement

We are given a string str, containing a total of N words. We need to find all palindrome words in a given string and create a new string by reversing the order of all palindrome words.

Example

enter

str = ‘nayan was gone to navjivan eye hospital’

Output

‘eye was gone to navjivan nayan hospital’

illustrate

The string contains three palindromes: nayan, navjivan and eye. We reversed the order of all three words and kept all other words the same.

enter

‘Hello, users! How are you?’

Output

‘Hello, users! How are you?’

illustrate

It gives the same output since the string does not contain any palindrome words.

enter

‘Your eye is beautiful.’

Output

‘Your eye is beautiful.’

illustrate

It gives the same output as a string containing only a single palindrome word.

method 1

In this method, we first split the string into words. After that, we will filter all palindrome words. Next, we reverse the order of all palindromes.

Finally, we iterate through the string and if the current word is a palindrome word, we replace it with another palindrome word in reverse order.

algorithm

  • Step 1 - Execute the reversePlaindromic() function by passing a string as argument that returns the result string.

  • Step 2 - Create isPalindrome() function to check if a word is a palindrome.

  • Step 2.1 - Initialize "start" to 0 and "end" to string length – 1.

  • Step 2.2 - Use a while loop to iterate through the string, comparing the first and last characters, comparing the second and penultimate characters, and so on. If any characters do not match, false is returned because it is not a palindrome string.

  • Step 2.3 - Returns true if the string is a palindrome.

  • Step 3 - Create a vector to store the words of the string. Additionally, define the "temp" variable to store the word.

  • Step 4 - Use a for loop to iterate over the string and append the character to the temporary value if it is not equal to a space (‘ ’). Otherwise, push the value of temp to the allWords vector.

  • Step 5 - Iterate over the allWords vector and check if the current word is a palindrome using the isPalindrome() function. If so, push the word into the "palindromWords" vector.

  • Step 6 - Invert the "palindromWords" list.

  • Step 7 - Now, iterate over the "allWords" vector again and check if the current word is a palindrome. If so, replace it with a respected word from the "palindromWords" list.

  • Step 8 - Iterate over the "palindromWords" list and create a string by appending all words to the result variable. Returns the result 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) since we iterate over strings of length N.

  • Space Complexity - O(K) because we use a list to store words, where k is the total number of words in the string.

in conclusion

We learned to take all the palindrome words from the sentence and add them in reverse order. In the above code, the programmer can try changing the implementation of the isPalindrome() function to learn something new.

The above is the detailed content of Modify the sentence by reversing the order in which all palindromic words appear. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete