Count palindromes java – Python Program to Count Palindrome Words in a Sentence

Count palindromes java: 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

Answer these:

  1. Count palindrome words in a sentence python?
  2. Count palindrome words in a sentence in java?
  3. Write a function to find all the words in a string which are palindrome in c#?
  4. Given a sentence write a program to count the number of palindrome words in it in c?
  5. Write a function to find all the words in a string which are palindrome in python?
  6. Write a function to find all the words in a string which are palindrome java?
  7. Write a function to find all the words in a string which are palindrome in c?
  8. Python program to count words in a sentence?
  9. Python program to count number of words in a paragraph?
  10. Python program to count words in a string?
  11. Python program to count number of words in a sentence?
  12. Palindrome count program in python?

Related Programs: