Write a python program to count the number of palindrome strings that occurred in the given string. Input:The input consists of a string.Output:count number of palindrome strings
Question
Write a python program to count the number of palindrome strings that occurred in the given string. Input:The input consists of a string.Output:count number of palindrome strings
Solution
Sure, here is a Python program that counts the number of palindrome strings in a given string:
def count_palindromes(input_string):
words = input_string.split(' ')
count = 0
for word in words:
if word == word[::-1]:
count += 1
return count
input_string = "madam arora teaches malayalam level"
print(count_palindromes(input_string))
This program works as follows:
- It defines a function
count_palindromesthat takes an input string as an argument. - The function splits the input string into words using the
splitmethod. - It initializes a count variable to 0.
- It then iterates over each word in the list of words.
- For each word, it checks if the word is the same as its reverse. If it is, it increments the count.
- After checking all the words, it returns the count.
- The program then calls this function with a test string and prints the result.
Similar Questions
Palindrome CountWrite a python program to count the number of palindrome strings that occurred in the given string. Input:The input consists of a string.Output:count number of palindrome stringsConstraints:1 < str < 10000Example:Input:madamOutput:2Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 madam2
Write a Python program to count the number of occurrences of a specific character in a string
Each palindrome can be always created from the other palindromes, if a single character is also a palindrome. For example, the string "bobseesanna" can be created by some ways:* bobseesanna = bob + sees + anna* bobseesanna = bob + s + ee + s + anna* bobseesanna = b + o + b + sees + a + n + n + a...We want to take the value of function CountPal(s) which is the number of different ways to use the palindromes to create the string s by the above method.InputThe string sOutputThe value of function CountPal(s), taking the modulo of 1 000 000 007 (109+7)Limitations0 < |s| <= 1000Sample 1:InputOutputbobseesanna18
Write a python program to check the given string is palindrome or not.
Define the function count_substring(string, substring) that finds how many times the substring appears in the given string.Example:string = "Python is a powerful language. I want to learn Python"substring = "Python"Expected Output:2
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.