Program to Count Non Palindrome words in a Sentence

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.