{"id":17348,"date":"2021-08-17T20:23:07","date_gmt":"2021-08-17T14:53:07","guid":{"rendered":"https:\/\/python-programs.com\/?p=17348"},"modified":"2021-11-22T18:37:21","modified_gmt":"2021-11-22T13:07:21","slug":"__trashed","status":"publish","type":"post","link":"https:\/\/python-programs.com\/__trashed\/","title":{"rendered":"Python Program to Sort Palindrome Words in a Sentence"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Sum of Digits of a Number at Even and Odd places<\/a> Palindrome:<\/strong><\/p>\n If the reverse of the given string is the same as the given original string, it is said to be a palindrome.<\/p>\n Example :<\/p>\n Given string = “sos asked to bring the madam pip “.<\/p>\n Output :<\/p>\n Explanation: In this “madam”, “pip”, “sos” are the palindromic words. By sorting them we get {“madam”, ‘pip’ , “sos”}<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to sort all the palindromic words in a given sentence:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation: Output:<\/strong><\/p>\n \u00a0<\/strong>Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n In the previous article, we have discussed Python Program to Find the Sum of Digits of a Number at Even and Odd places Given a string and the task is to sort all the palindrome words in a given sentence. Palindrome: If the reverse of the given string is the same as the given original …<\/p>\n
\nGiven a string and the task is to sort all the palindrome words in a given sentence.<\/p>\nGiven string\/sentence ='sos how are you madam pip instal'<\/pre>\n
The given string before sorting all the palindromic words is = sos how are you madam pip instal\r\nThe final string after sorting all the palindromic words is = madam how are you pip sos instal<\/pre>\n
Given string\/sentence = 'the good is madam aba dad mom din cac'<\/pre>\n
The given string before sorting all the palindromic words is = the good is madam aba dad mom din cac\r\nThe final string after sorting all the palindromic words is = the good is aba cac dad madam din mom<\/pre>\n
Program to Sort Palindrome Words in a Sentence in Python<\/h2>\n
\n
Method #1: Using Sort() function (Static input)<\/h3>\n
\n
# Give the sentence\/string as static input and store it in a variable.\r\ngvnstrng = 'sos how are you madam pip instal'\r\n# Convert the given sentence to a list of words using list()\r\n# and split() functions and store it another variable.\r\nstrngwrdslst = list(gvnstrng.split())\r\n# Take an empty list to say palindromicwordslist\r\n# that stores all the palindromic words in the given string\r\n# and initialize it to null\/empty using the list() function or [].\r\npalindromicwordslist = []\r\n# Traverse the given list of words using a for loop.\r\nfor wrd in strngwrdslst:\r\n # Check if the word is palindrome or not using the slicing\r\n # and if conditional statement.\r\n if(wrd == wrd[::-1]):\r\n # If it is true then append this word to the palindromicwordslist\r\n # using the append() function.\r\n palindromicwordslist.append(wrd)\r\n\r\n# Sort the palindromicwordslist using the sort() function.\r\npalindromicwordslist.sort()\r\n# Take a variable say tempo and initialize its value to 0\r\n# (Here it acts as a pointer to palindromicwordslist ).\r\ntempo = 0\r\n# Traverse the list of words of the given sentence using the For loop.\r\nfor wrditr in range(len(strngwrdslst)):\r\n # Check if the word is palindrome or not using the slicing\r\n # and if conditional statement.\r\n if(strngwrdslst[wrditr] == strngwrdslst[wrditr][::-1]):\r\n # If it is true then modify the word with the palindromicwordslist[tempo]\u00a0word.\r\n strngwrdslst[wrditr] = palindromicwordslist[tempo]\r\n tempo = tempo+1\r\n # Increment the tempo value by 1.\r\n\r\n\r\n# Convert this list of words of the given sentence\r\n# to the string using the join() function.\r\nfinalstrng = ' '.join(strngwrdslst)\r\nprint('The given string before sorting all the palindromic words is = ', gvnstrng)\r\n# Print the final string after sorting the palindromic words.\r\nprint('The final string after sorting all the palindromic words is = ', finalstrng)\r\n<\/pre>\n
The given string before sorting all the palindromic words is = sos how are you madam pip instal\r\nThe final string after sorting all the palindromic words is = madam how are you pip sos instal<\/pre>\n
Method #2:Using Sort() function (User input)<\/h3>\n
\n
\n<\/strong><\/p>\n# Give the sentence\/string as user input using input() function\r\n# and store it in a variable.\r\ngvnstrng = input('Enter some random string = ')\r\n# Convert the given sentence to a list of words using list()\r\n# and split() functions and store it another variable.\r\nstrngwrdslst = list(gvnstrng.split())\r\n# Take an empty list to say palindromicwordslist\r\n# that stores all the palindromic words in the given string\r\n# and initialize it to null\/empty using the list() function or [].\r\npalindromicwordslist = []\r\n# Traverse the given list of words using a for loop.\r\nfor wrd in strngwrdslst:\r\n # Check if the word is palindrome or not using the slicing\r\n # and if conditional statement.\r\n if(wrd == wrd[::-1]):\r\n # If it is true then append this word to the palindromicwordslist\r\n # using the append() function.\r\n palindromicwordslist.append(wrd)\r\n\r\n# Sort the palindromicwordslist using the sort() function.\r\npalindromicwordslist.sort()\r\n# Take a variable say tempo and initialize its value to 0\r\n# (Here it acts as a pointer to palindromicwordslist ).\r\ntempo = 0\r\n# Traverse the list of words of the given sentence using the For loop.\r\nfor wrditr in range(len(strngwrdslst)):\r\n # Check if the word is palindrome or not using the slicing\r\n # and if conditional statement.\r\n if(strngwrdslst[wrditr] == strngwrdslst[wrditr][::-1]):\r\n # If it is true then modify the word with the palindromicwordslist[tempo]\u00a0word.\r\n strngwrdslst[wrditr] = palindromicwordslist[tempo]\r\n tempo = tempo+1\r\n # Increment the tempo value by 1.\r\n\r\n\r\n# Convert this list of words of the given sentence\r\n# to the string using the join() function.\r\nfinalstrng = ' '.join(strngwrdslst)\r\nprint('The given string before sorting all the palindromic words is = ', gvnstrng)\r\n# Print the final string after sorting the palindromic words.\r\nprint('The final string after sorting all the palindromic words is = ', finalstrng)\r\n<\/pre>\n
Enter some random string = the good is madam aba dad mom din cac\r\nThe given string before sorting all the palindromic words is = the good is madam aba dad mom din cac\r\nThe final string after sorting all the palindromic words is = the good is aba cac dad madam din mom<\/pre>\n
\n