{"id":17545,"date":"2021-08-26T10:42:25","date_gmt":"2021-08-26T05:12:25","guid":{"rendered":"https:\/\/python-programs.com\/?p=17545"},"modified":"2021-11-22T18:37:12","modified_gmt":"2021-11-22T13:07:12","slug":"python-program-to-count-non-palindrome-words-in-a-sentence","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-non-palindrome-words-in-a-sentence\/","title":{"rendered":"Python Program to Count Non Palindrome words in a Sentence"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Leaders in an Array\/List<\/a> Palindrome:<\/strong><\/p>\n If the reverse of a string is the same as the string, it is said to be a palindrome.<\/p>\n Example :<\/p>\n Given string = “sos asked to bring the madam “.<\/p>\n Output :<\/p>\n Explanation: In this “madam”, “sos” are the palindromic words. By sorting them we get {“madam”,”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 count all the Non-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:<\/strong><\/p>\n Output:<\/strong><\/p>\n 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 Leaders in an Array\/List Given a string and the task is to count all the Non-palindromic words in a given sentence. Palindrome: If the reverse of a string is the same as the string, it is said to be a palindrome. Example : Given …<\/p>\n
\nGiven a string and the task is to count all the Non-palindromic words in a given sentence.<\/p>\nGiven String = \"dad and mom both ordered to bring sos in malayalam\"<\/pre>\n
The count of all the Non-palindromic words in a given sentence = 6<\/pre>\n
Given String = \"My mom and dad treats me in equal level\"<\/pre>\n
The count of all the Non-palindromic words in a given sentence = 6<\/pre>\n
Program to Count Non-Palindrome words in a Sentence<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"dad and mom both ordered to bring sos in malayalam\"\r\n# Take an empty list and store it in another variable.\r\nlst = []\r\n# Split the given string using the split() function and store it in another variable.\r\nsplt_str = gvn_str.split()\r\n# Loop in the above-obtained split list of words using the for loop.\r\nfor wrd in splt_str:\r\n # Check if the iterator value is not equal to the reverse of the iterator value using\r\n # the if conditional statement.\r\n if wrd != wrd[::-1]:\r\n # If the statement is true, then append the respective iterator value to the\r\n # above initialized empty list using the append() method.\r\n lst.append(wrd)\r\n# Calculate the length above initialized list \"lst\" using the len() function\r\n# and store it in a variable.\r\n# Print the count of all the Non-palindromic words in a given sentence.\r\ncount = len(lst)\r\n# Print the count of all the Non-palindromic words in a given sentence.\r\nprint(\"The count of all the Non-palindromic words in a given sentence =\", count)\r\n<\/pre>\n
The count of all the Non-palindromic words in a given sentence = 6<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Take an empty list and store it in another variable.\r\nlst = []\r\n# Split the given string using the split() function and store it in another variable.\r\nsplt_str = gvn_str.split()\r\n# Loop in the above-obtained split list of words using the for loop.\r\nfor wrd in splt_str:\r\n # Check if the iterator value is notequal to the reverse of the iterator value using\r\n # the if conditional statement.\r\n if wrd != wrd[::-1]:\r\n # If the statement is true, then append the respective iterator value to the\r\n # above initialized empty list using the append() method.\r\n lst.append(wrd)\r\n# Calculate the length above initialized list \"lst\" using the len() function\r\n# and store it in a variable.\r\n# Print the count of all the Non-palindromic words in a given sentence.\r\ncount = len(lst)\r\n# Print the count of all the Non-palindromic words in a given sentence.\r\nprint(\"The count of all the Non-palindromic words in a given sentence =\", count)\r\n<\/pre>\n
Enter some random string = My mom and dad treats me in equal level\r\nThe count of all the Non-palindromic words in a given sentence = 6<\/pre>\n
\n