Count palindrome words in a sentence in C++


We are given a string containing an English sentence. The goal is to find the number of words in the string that are palindromes. Palindrome words are those that when read from start or end have the same alphabet sequence. If the sentence is “Madam speaks good Malayalam”, then count of palindrome words is 2. (Madam and Malayalam)

Note − Words can contain both upper and lowercase alphabets.

Let us understand with examples.

Input − str = "My Mom and Anna left at Noon";

Output − Count of palindrome words in a sentence are − 3

Explanation − Palindrome words in above sentence are − Mom, Anna and Noon. (irrespective of cases of alphabet)

Input − str= “I am at level 121 in Racecar game”

Output − Count of palindrome words in a sentence are − 4

Explanation − Palindrome words in above sentence are − I, level, 121, Racecar. (irrespective of cases of alphabet)

Approach used in the below program is as follows

We will take each word after a space “ “ in a sentence and pass it to a function. The function transforms the characters of words in lowercase. Now start traversing from the first character of the word and compare word[0] with word[length-1], word[1] with word[length-2] and so on. If any mismatch occurs break loop else return true.

  • Take a string array str[] containing a sentence.

  • Function check(string extra) takes a string and returns true if the string is palindrome else returns false.

  • Calculate length of string extra as len=extra.lenght().

  • Convert whole string to lowercase using (extra.begin(), extra.end(), extra.begin(), ::tolower);

  • Start traversing from index 0 of word till index<len using for loop.

  • Compare extra[i]==extra[len-1]. If mismatch occurs return false. Else return true.

  • Function palindrome(string str, int length) takes a sentence and its length and returns the number of palindrome words in it.

  • Take the initial count as 0.

  • Take temporary string extra=”” to pick and store individual words.

  • Start traversing the sentence using for loop from index 0 till i<length.

  • Take temporary character temp=str.ar(i).

  • If temp is not space add it to extra to make a word.

  • If temp is not space then if (check(extra)) returns true increment count.

  • Make extra=”” again.

  • The last count will have a total number of palindrome words.

  • Return count as result.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
bool check(string extra){
   int len = extra.length();
   transform(extra.begin(), extra.end(), extra.begin(), ::tolower);
   for (int i = 0; i < len; i++,len--){
      if (extra.at(i) != extra.at(len - 1)){
         return false;
      }
   }
   return true;
}
int palindrome(string str, int length){
   int count = 0;
   string extra = "";
   for (int i = 0; i < length; i++){
      char temp = str.at(i);
      if (temp != ' '){
         extra = extra + temp;
      }
      else{
         if (check(extra))
            { count++; }
         extra = "";
      }
   }
   return count;
}
int main(){
   string str = "nitin wants nitin for his company named nitin after nitin";
   str = str + " ";
   int length = str.length();
   cout<<"Count of palindrome words in a sentence are: "<<palindrome(str, length)<<endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of palindrome words in a sentence are: 4

Updated on: 02-Dec-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements