Count Palindrome Words in a String

Given a string 'S' consisting of words, your task is to determine the number of palindrome words within 'S'. A word is considered a palindrome if it reads the same backward as it does forward.

Input:

The first line contains an integer 'T' representing the number of test cases. Each test case is given by a single line containing the string 'S'.

Output:

For each test case, output the number of palindrome words found in the string 'S', one per line.

Example:

Input:
2
Madam oyo cat
Was it a car or a cat I saw
Output:
2
3

Constraints:

  • 1 <= T <= 10
  • 0 <= |S| <= 10^5
  • The string 'S' contains only whitespace, lowercase, and uppercase English letters.

Note:

Words are separated by one or more whitespace characters.
AnswerBot
4mo

Count the number of palindrome words in a given string.

  • Split the string into words using whitespace as delimiter.

  • Check each word if it is a palindrome by comparing it with its reverse.

  • Increment a coun...read more

Help your peers!
Select
Add answer anonymously...

Top Technical Trainee Interview Questions Asked at Nagarro

Q. Counting Derangements Problem A derangement is a permutation of 'N' elements, wh...read more
Q. Detect and Remove Loop in Linked List For a given singly linked list, identify i...read more
Q. Character Formation Check Determine if the second string STR2 can be constructed...read more
Technical Trainee Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits