Python Program to Reverse the Order of Palindrome Words in a Sentence

In the previous article, we have discussed Python Program to Sort Palindrome Words in a Sentence
Given a string and the task is to reverse the order of all the palindromic words in a given sentence in Python

Palindrome:

If the reverse of the given string is the same as the given original string, it is said to be a palindrome.

Examples:

Example1:

Input:

Given string/sentence = 'good bob how are you dod mom'

Output:

The given string before reversing the order of paindromic words is =  good bob how are you dod mom
The given string after reversing the order of paindromic words is =  good mom how are you dod bob

Example2:

Input:

Given string/sentence = 'hello madam how are yoou mom'

Output:

The given string before reversing the order of paindromic words is = hello madam how are yoou mom
The given string after reversing the order of paindromic words is = hello mom how are yoou madam

Program to Reverse the Order of Palindrome Words in a Sentence in Python

Below are the ways to reverse the order of all the palindromic words in a given sentence:

Method #1: Using reverse() function (Static input)

Approach:

  • Give the sentence/string as static input and store it in a variable.
  • Convert the given sentence to a list of words using list() and split() functions and store it another variable.
  • Take an empty list to say palindromicwordslist that stores all the palindromic words in the given string and initialize it to null/empty using the list() function or [].
  • Traverse the given list of words using a for loop.
  • Check if the word is palindrome or not using the slicing and if conditional statement.
  • If it is true then append this word to the palindromicwordslist using the append() function.
  • Reverse the palindromicwordslist using the reverse() function.
  • Take a variable say tempo and initialize its value to 0(Here it acts as a pointer to palindromicwordslist ).
  • Traverse the list of words of the given sentence using the For loop.
  • Check if the word is palindrome or not using the slicing and if conditional statement.
  • If it is true then modify the word with the palindromicwordslist[tempo] word.
  • Increment the tempo value by 1.
  • Convert this list of words of the given sentence to the string using the join() function.
  • Print the final string after sorting the palindromic words.
  • The Exit of the Program.

Below is the implementation:

# Give the sentence/string as static input and store it in a variable.
gvnstrng = 'good bob how are you dod mom'
# Convert the given sentence to a list of words using list()
# and split() functions and store it another variable.
strngwrdslst = list(gvnstrng.split())
# Take an empty list to say palindromicwordslist
# that stores all the palindromic words in the given string
# and initialize it to null/empty using the list() function or [].
palindromicwordslist = []
# Traverse the given list of words using a for loop.
for wrd in strngwrdslst:
        # Check if the word is palindrome or not using the slicing
    # and if conditional statement.
    if(wrd == wrd[::-1]):
        # If it is true then append this word to the palindromicwordslist
        # using the append() function.
        palindromicwordslist.append(wrd)

# Reverse the palindromicwordslist using the reverse() function.
palindromicwordslist.reverse()
# Take a variable say tempo and initialize its value to 0
# (Here it acts as a pointer to palindromicwordslist ).
tempo = 0
# Traverse the list of words of the given sentence using the For loop.
for wrditr in range(len(strngwrdslst)):
  # Check if the word is palindrome or not using the slicing
    # and if conditional statement.
    if(strngwrdslst[wrditr] == strngwrdslst[wrditr][::-1]):
        # If it is true then modify the word with the palindromicwordslist[tempo] word.
        strngwrdslst[wrditr] = palindromicwordslist[tempo]
        tempo = tempo+1
        # Increment the tempo value by 1.


# Convert this list of words of the given sentence
# to the string using the join() function.
finalstrng = ' '.join(strngwrdslst)
print('The given string before reversing the order of paindromic words is = ', gvnstrng)
# Print the final string after reversing the palindromic words.
print('The given string after reversing the order of paindromic words is = ', finalstrng)

Output:

The given string before reversing the order of paindromic words is =  good bob how are you dod mom
The given string after reversing the order of paindromic words is =  good mom how are you dod bob

Method #2: Using reverse() function (User input)

Approach:

  • Give the sentence/string as user input using the input() function and store it in a variable.
  • Convert the given sentence to a list of words using list() and split() functions and store it another variable.
  • Take an empty list to say palindromicwordslist that stores all the palindromic words in the given string and initialize it to null/empty using the list() function or [].
  • Traverse the given list of words using a for loop.
  • Check if the word is palindrome or not using the slicing and if conditional statement.
  • If it is true then append this word to the palindromicwordslist using the append() function.
  • Reverse the palindromicwordslist using the reverse() function.
  • Take a variable say tempo and initialize its value to 0(Here it acts as a pointer to palindromicwordslist ).
  • Traverse the list of words of the given sentence using the For loop.
  • Check if the word is palindrome or not using the slicing and if conditional statement.
  • If it is true then modify the word with the palindromicwordslist[tempo] word.
  • Increment the tempo value by 1.
  • Convert this list of words of the given sentence to the string using the join() function.
  • Print the final string after sorting the palindromic words.
  • The Exit of the Program.

Below is the implementation:

# Give the sentence/string as user input using input() function
# and store it in a variable.
gvnstrng = input('Enter some random string = ')
# Convert the given sentence to a list of words using list()
# and split() functions and store it another variable.
strngwrdslst = list(gvnstrng.split())
# Take an empty list to say palindromicwordslist
# that stores all the palindromic words in the given string
# and initialize it to null/empty using the list() function or [].
palindromicwordslist = []
# Traverse the given list of words using a for loop.
for wrd in strngwrdslst:
        # Check if the word is palindrome or not using the slicing
    # and if conditional statement.
    if(wrd == wrd[::-1]):
        # If it is true then append this word to the palindromicwordslist
        # using the append() function.
        palindromicwordslist.append(wrd)


# Reverse the palindromicwordslist using the reverse() function.
palindromicwordslist.reverse()
# Take a variable say tempo and initialize its value to 0
# (Here it acts as a pointer to palindromicwordslist ).
tempo = 0
# Traverse the list of words of the given sentence using the For loop.
for wrditr in range(len(strngwrdslst)):
  # Check if the word is palindrome or not using the slicing
    # and if conditional statement.
    if(strngwrdslst[wrditr] == strngwrdslst[wrditr][::-1]):
        # If it is true then modify the word with the palindromicwordslist[tempo] word.
        strngwrdslst[wrditr] = palindromicwordslist[tempo]
        tempo = tempo+1
        # Increment the tempo value by 1.


# Convert this list of words of the given sentence
# to the string using the join() function.
finalstrng = ' '.join(strngwrdslst)
print('The given string before reversing the order of paindromic words is = ', gvnstrng)
# Print the final string after reversing the palindromic words.
print('The given string after reversing the order of paindromic words is = ', finalstrng)

Output:

Enter some random string = hello madam how are yoou mom
The given string before reversing the order of paindromic words is = hello madam how are yoou mom
The given string after reversing the order of paindromic words is = hello mom how are yoou madam

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.