Program to Count Palindrome Words in a Sentence

Python Program to Count Palindrome Words in a Sentence

Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.

Given a sentence/string the task is to count the number of palindromic words in the given string in Python.

Examples:

Example1:

Input:

Given Sentence =madam how are you

Output:

The total number of palindromic words in the given sentence { madam how are you } are 1

Example2:

Input:

Given Sentence = helleh this issi btechgeeksskeeghcetb pyyp

Output:

The total number of palindromic words in the given sentence { helleh this issi btechgeeksskeeghcetb pyyp } are  4

Program to Count Palindrome Words in a Sentence in Python

Below are the ways to Count Palindrome words in the given sentence in Python

Method #1: Using For Loop (Static Input)

Approach:

  • Give the sentence/string as static input and store it in a variable.
  • Take a variable palcnt which stores the count of palindromic words in the given sentence and initialize its value to 0.
  • Split the given sentence into a list of words using the built-in function split() and store it in a variable.
  • Traverse in this list of words using the For loop.
  • Inside the for loop,Reverse the iterator word using slicing and store it in a variable.
  • Check if this reverse word is equal to the iterator word(Palindrome Condition) using the If conditional statement.
  • If it is true then increment the value of palcnt by 1.
  • Print the palcnt value.
  • The Exit of the Program.

Below is the implementation:

# Give the sentence/string as static input and store it in a variable.
gvnsentence ="helleh this issi btechgeeksskeeghcetb pyyp"
# Take a variable palcnt which stores the count of palindromic words
# in the given sentence and initialize its value to 0.
palcnt = 0
# Split the given sentence into a list of words using
# the built-in function split() and store it in a variable.
sentcewords = gvnsentence.split()
# Traverse in this list of words using the For loop.
for itrword in sentcewords:
    # Inside the for loop,Reverse the iterator word
    # using slicing and store it in a variable.
    reveword = itrword[::-1]
    # Check if this reverse word is equal to the
    # iterator word(Palindrome Condition) using the If conditional statement.
    if(reveword == itrword):
        # If it is true then increment the value of palcnt by 1.
        palcnt = palcnt+1


# Print the palcnt value.
print(
    'The total number of palindromic words in the given sentence {', gvnsentence, '} are ', palcnt)

Output:

The total number of palindromic words in the given sentence { helleh this issi btechgeeksskeeghcetb pyyp } are  4

Method #2: Using For Loop (User Input)

Approach:

  • Give the sentence/string as user input using the input() function and store it in a variable.
  • Take a variable palcnt which stores the count of palindromic words in the given sentence and initialize its value to 0.
  • Split the given sentence into a list of words using the built-in function split() and store it in a variable.
  • Traverse in this list of words using the For loop.
  • Inside the for loop,Reverse the iterator word using slicing and store it in a variable.
  • Check if this reverse word is equal to the iterator word(Palindrome Condition) using the If conditional statement.
  • If it is true then increment the value of palcnt by 1.
  • Print the palcnt value.
  • The Exit of the Program.

Below is the implementation:

# Give the sentence/string as user input using the input() function and store it in a variable.
gvnsentence = input('Enter some random sentence =')
# Take a variable palcnt which stores the count of palindromic words
# in the given sentence and initialize its value to 0.
palcnt = 0
# Split the given sentence into a list of words using
# the built-in function split() and store it in a variable.
sentcewords = gvnsentence.split()
# Traverse in this list of words using the For loop.
for itrword in sentcewords:
    # Inside the for loop,Reverse the iterator word
    # using slicing and store it in a variable.
    reveword = itrword[::-1]
    # Check if this reverse word is equal to the
    # iterator word(Palindrome Condition) using the If conditional statement.
    if(reveword == itrword):
        # If it is true then increment the value of palcnt by 1.
        palcnt = palcnt+1


# Print the palcnt value.
print(
    'The total number of palindromic words in the given sentence {', gvnsentence, '} are ', palcnt)

Output:

Enter some random sentence =madam how are you
The total number of palindromic words in the given sentence { madam how are you } are 1

Related Programs: