Sentence Palindrome (Palindrome after removing spaces, dots, .. etc) - GeeksforGeeks

14 Mar, 2022
Write a program to check if a sentence is a palindrome or not. You can ignore white spaces and other characters to consider sentences as palindrome.Examples: 
Input : str = "Too hot to hoot."
Output : Sentence is palindrome.

Input : str = "Abc def ghi jklm."
Output : Sentence is not palindrome.
Note that there may be multiple spaces and/or dots between two words. 
To find if a sentence is palindrome, compare each character from left and right. If they are equal, compare until left and right of string are equal or right becomes less than left. Remember to ignore white spaces and other characters in a string.  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find if a sentence is// palindrome#include <bits/stdc++.h>using namespace std;  // To check sentence is palindrome or notbool sentencePalindrome(string str){    int l = 0, h = str.length() - 1;      // Lowercase string    for (int i = 0; i <= h; i++)        str[i] = tolower(str[i]);      // Compares character until they are equal    while (l <= h) {          // If there is another symbol in left        // of sentence        if (!(str[l] >= 'a' && str[l] <= 'z'))            l++;          // If there is another symbol in right        // of sentence        else if (!(str[h] >= 'a' && str[h] <= 'z'))            h--;          // If characters are equal        else if (str[l] == str[h])            l++, h--;          // If characters are not equal then        // sentence is not palindrome        else            return false;    }      // Returns true if sentence is palindrome    return true;}  // Driver program to test sentencePalindrome()int main(){    string str = "Too hot to hoot.";      if (sentencePalindrome(str))        cout << "Sentence is palindrome.";    else        cout << "Sentence is not palindrome.";      return 0;}
// Java program to find if a sentence is// palindromepublic class GFG{    // To check sentence is palindrome or not    static boolean sentencePalindrome(String str)    {           int l = 0;        int h = str.length()-1;                 // Lowercase string        str = str.toLowerCase();                 // Compares character until they are equal        while(l <= h)        {                         char getAtl = str.charAt(l);            char getAth = str.charAt(h);                         // If there is another symbol in left            // of sentence            if (!(getAtl >= 'a' && getAtl <= 'z'))                l++;                         // If there is another symbol in right            // of sentence            else if(!(getAth >= 'a' && getAth <= 'z'))                h--;                         // If characters are equal            else if( getAtl == getAth)            {                l++;                h--;            }                         // If characters are not equal then            // sentence is not palindrome            else                return false;        }                 // Returns true if sentence is palindrome        return true;       }         // Driver program to test sentencePallindrome()    public static void main(String[] args)    {        String str = "Too hot to hoot.";        if( sentencePalindrome(str))          System.out.println("Sentence is palindrome");        else          System.out.println("Sentence is not" + " " +                                         "palindrome");    }} //This code is contributed by Sumit Ghosh
# Python program to find if a sentence is# palindrome# To check sentence is palindrome or notdef sentencePalindrome(s):    l, h = 0, len(s) - 1       # Lowercase string    s = s.lower()       # Compares character until they are equal    while (l <= h):           # If there is another symbol in left        # of sentence        if (not(s[l] >= 'a' and s[l] <= 'z')):            l += 1           # If there is another symbol in right        # of sentence        else if (not(s[h] >= 'a' and s[h] <= 'z')):            h -= 1           # If characters are equal        else if (s[l] == s[h]):            l += 1            h -= 1                 # If characters are not equal then        # sentence is not palindrome        else:            return False    # Returns true if sentence is palindrome    return True   # Driver program to test sentencePalindrome()s = "Too hot to hoot."if (sentencePalindrome(s)):    print ("Sentence is palindrome.")else:    print ("Sentence is not palindrome.") # This code is contributed by Sachin Bisht
// C# program to find if a// sentence is palindromeusing System; public class GFG{         // To check sentence is    // palindrome or not    static bool sentencePalindrome(String str)    {        int l = 0;        int h = str.Length - 1;                 // Lowercase string        str = str.ToLower();                 // Compares character until        // they are equal        while(l <= h)        {                         char getAtl = str[l];            char getAth = str[h];                         // If there is another symbol            // in left of sentence            if (!(getAtl >= 'a' &&                  getAtl <= 'z'))                l++;                         // If there is another symbol             // in right of sentence            else if(! (getAth >= 'a' &&                       getAth <= 'z'))                h--;                         // If characters are equal            else if( getAtl == getAth)            {                l++;                h--;            }                         // If characters are not equal then            // sentence is not palindrome            else                return false;        }                 // Returns true if sentence        // is palindrome        return true;    }         // Driver Code    public static void Main()    {        String str = "Too hot to hoot.";        if( sentencePalindrome(str))        Console.Write("Sentence is palindrome");        else        Console.Write("Sentence is not" + " " +                                     "palindrome");    }} // This code is contributed by Nitin Mittal.
<?php// PHP program to find if a sentence is// palindrome   // To check sentence is palindrome or notfunction sentencePalindrome($str){    $l = 0;    $h = strlen($str)-1;       // Lowercase string    for ($i = 0; $i < $h; $i++)        $str[$i] = strtolower($str[$i]);       // Compares character until they are equal    while ($l <= $h) {           // If there is another symbol in left        // of sentence        if (!($str[$l] >= 'a' && $str[$l] <= 'z'))            $l++;           // If there is another symbol in right        // of sentence        else if (!($str[$h] >= 'a' && $str[$h] <= 'z'))            $h--;           // If characters are equal        else if ($str[$l] == $str[$h])        {             $l++;            $h--;        }           // If characters are not equal then        // sentence is not palindrome        else            return false;    }       // Returns true if sentence is palindrome    return true;}   // Driver program to test sentencePalindrome() $str = "Too hot to hoot."; if (sentencePalindrome($str))    echo "Sentence is palindrome.";else    echo "Sentence is not palindrome."; return 0;?>
<script>// Javascript program to find if a sentence is// palindrome         // To check sentence is palindrome or not    function sentencePalindrome(str)    {        let l = 0;        let h = str.length-1;                  // Lowercase string        str = str.toLowerCase();                  // Compares character until they are equal        while(l <= h)        {                          let getAtl = str[l];            let getAth = str[h];                          // If there is another symbol in left            // of sentence            if (!(getAtl >= 'a' && getAtl <= 'z'))                l++;                          // If there is another symbol in right            // of sentence            else if(!(getAth >= 'a' && getAth <= 'z'))                h--;                          // If characters are equal            else if( getAtl == getAth)            {                l++;                h--;            }                          // If characters are not equal then            // sentence is not palindrome            else                return false;        }                  // Returns true if sentence is palindrome        return true;      }         // Driver program to test sentencePallindrome()    let str = "Too hot to hoot.";    if( sentencePalindrome(str))        document.write("Sentence is palindrome");    else        document.write("Sentence is not" + " " +                                         "palindrome");              //This code is contributed by avanitrachhadiya2155     </script>
Output: 
Sentence is palindrome.
Time Complexity: O(N) where N is the length of the sentenceSpace Complexity: O(1) 
This article is contributed by nuclode. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
ukasp
shivanshuayachi
avanitrachhadiya2155
CoderSaty
amartyaghoshgfg
surinderdawra388
palindrome
Strings
Strings
palindrome
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Check for Balanced Brackets in an expression (well-formedness) using Stack
KMP Algorithm for Pattern Searching
Convert string to char array in C++
Array of Strings in C++ (5 Different Ways to Create)
Longest Palindromic Substring | Set 1
Caesar Cipher in Cryptography
Length of the longest substring without repeating characters
Check whether two strings are anagram of each other
How to split a string in C/C++, Python and Java?
Remove duplicates from a given string