title: How can I print the palindrome words in a sentence? If I input "Madam Arora teaches math", it should print "Madam Arora"
body: <pre class="lang-java prettyprint-override"><code>class Palindrome { 
        
      // Function to check if a word is 
      // palindrome 
      static boolean checkPalin(String word) 
      { 
          int n = word.length(); 
          word = word.toLowerCase(); 
          for (int i=0; i&lt;n; i++,n--) 
             if (word.charAt(i) != word.charAt(n - 1)) 
                return false;        
          return true; 
      } 
        
      // Function to count palindrome words 
      static int countPalin(String str) 
      {         
          // to check last word for palindrome 
          str = str + &quot; &quot;; 
            
          // to store each word 
          String word = &quot;&quot;; 
          int count = 0; 
          for (int i = 0; i &lt; str.length(); i++) 
          { 
              char ch = str.charAt(i); 
                
              // extracting each word 
              if (ch != ' ') 
                  word = word + ch; 
              else { 
                  if (checkPalin(word)) 
                      count++; 
                  word = &quot;&quot;; 
              } 
          } 
            
          return count; 
      } 
        
      // Driver code 
      public static void main(String args[]) 
      { 
          System.out.println(countPalin(&quot;Madam &quot;
                    + &quot;Arora teaches malayalam&quot;)); 
                      
          System.out.println(countPalin(&quot;Nitin &quot;
                          + &quot;speaks malayalam&quot;)); 
      }
  }
  </code></pre>
  <p>I only know how to count the number. How can I run through a loop and check every word if it is a palindrome? Can you help me with a method that checks a word and a method that checks a sentence if there are palindrome words? I must use Stacks and Queues. I have done the push, add, remove, dequeue, but I can't check if the input is a sentence.</p>