Python Program to Count Non Palindrome words in a Sentence

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 string = “sos asked to bring the madam “.

Output :

Explanation: In this “madam”, “sos” are the palindromic words. By sorting them we get {“madam”,”sos”}

Examples:

Example1:

Input:

Given String = "dad and mom both ordered to bring sos in malayalam"

Output:

The count of all the Non-palindromic words in a given sentence = 6

Example2:

Input:

Given String = "My mom and dad treats me in equal level"

Output:

The count of all the Non-palindromic words in a given sentence = 6

Program to Count Non-Palindrome words in a Sentence

Below are the ways to count all the Non-palindromic words in a given sentence.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Take an empty list say “lst” and store it in another variable.
  • Split the given string using the split() function and store it in another variable.
  • Loop in the above-obtained split list of words using the for loop.
  • Check if the iterator value is not equal to the reverse of the iterator value using the if conditional statement.
  • If the statement is true, then append the respective iterator value to the above initialized empty list using the append() method.
  • Calculate the length above initialized list “lst” using the len() function and store it in a variable.
  • Print the count of all the Non-palindromic words in a given sentence.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "dad and mom both ordered to bring sos in malayalam"
# Take an empty list and store it in another variable.
lst = []
# Split the given string using the split() function and store it in another variable.
splt_str = gvn_str.split()
# Loop in the above-obtained split list of words using the for loop.
for wrd in splt_str:
    # Check if the iterator value is not equal to the reverse of the iterator value using
    # the if conditional statement.
    if wrd != wrd[::-1]:
     # If the statement is true, then append the respective iterator value to the
        # above initialized empty list using the append() method.
        lst.append(wrd)
# Calculate the length above initialized list "lst" using the len() function
# and store it in a variable.
# Print the count of all the Non-palindromic words in a given sentence.
count = len(lst)
# Print the count of all the Non-palindromic words in a given sentence.
print("The count of all the Non-palindromic words in a given sentence =", count)

Output:

The count of all the Non-palindromic words in a given sentence = 6

Method #2: Using For loop (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Take an empty list say “lst” and store it in another variable.
  • Split the given string using the split() function and store it in another variable.
  • Loop in the above-obtained split list of words using the for loop.
  • Check if the iterator value is not equal to the reverse of the iterator value using the if conditional statement.
  • If the statement is true, then append the respective iterator value to the above initialized empty list using the append() method.
  • Calculate the length above initialized list “lst” using the len() function and store it in a variable.
  • Print the count of all the Non-palindromic words in a given sentence.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Take an empty list and store it in another variable.
lst = []
# Split the given string using the split() function and store it in another variable.
splt_str = gvn_str.split()
# Loop in the above-obtained split list of words using the for loop.
for wrd in splt_str:
    # Check if the iterator value is notequal to the reverse of the iterator value using
    # the if conditional statement.
    if wrd != wrd[::-1]:
     # If the statement is true, then append the respective iterator value to the
        # above initialized empty list using the append() method.
        lst.append(wrd)
# Calculate the length above initialized list "lst" using the len() function
# and store it in a variable.
# Print the count of all the Non-palindromic words in a given sentence.
count = len(lst)
# Print the count of all the Non-palindromic words in a given sentence.
print("The count of all the Non-palindromic words in a given sentence =", count)

Output:

Enter some random string = My mom and dad treats me in equal level
The count of all the Non-palindromic words in a given sentence = 6

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.

Test Yourself:

  1. Exclude palindrome words in python
  2. Qrite a function to find all the words in a string which are palindrome in python
  3. Python program to count no of words in a string
  4. Python program to count number of words in a sentence
  5. Python program to count words in a sentence
  6. Python program to count number of words in a paragraph
  7. Count palindrome words in a sentence python
  8. Palindrome count program in python
  9. Java program to print palindrome words in a string?
  10. Count palindrome words in a sentence in c
  11. Count palindrome words in a sentence python
  12. Palindrome program in python using function?

Related Posts: