Count Palindrome Words in a string

You are given a string s of words. Your task is to find the number of palindrome words in the given string S. A word is called palindrome if it read the same from backward as well as forward. Words are separated by one or more whitespaces

problem- When I am giving string only as “Deed” it is giving 1 which is correct but when I am giving string as “I Deed” it is showing 1 but it should show 2.
I have attached the code below.

char toLowerCase(char ch)
{
    if(ch>='A' && ch<='Z')
    {
        ch=ch-'A'+'a';
    }
    return ch;
}
int countNumberOfPalindromeWords(string s)
{
    //Your code goes here
    int st=0,e=0;
    int lengt=s.length();
    int cnt=0;
    while(st<lengt && e<lengt)
    {
        if(s[e]!=' ')
        {
            string temp="";
        while(e<lengt && s[e]!=' ')
        {
         temp+=toLowerCase(s[e]);
         e++;
        }

        int i=st;
        int j=e-1;
        bool isPalindrome=true;
        while(i<=j)
        {
            if(temp[i]!=temp[j])
            {
                isPalindrome=false;
                break;
            }
            i++;
            j--;
        }
        if(isPalindrome)
        {
            cnt++;
        }
        st=e;
        
        }
        else{
            e++;
            st++;

        }

    }
    return cnt;
}```

Leave a Comment