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 then left. Remember to ignore white spaces and other characters in a string.

C++

filter_none

editclose

play_arrow

linkbrightness_4code

// CPP program to find if a sentence is

// palindrome

#include <bits/stdc++.h>

usingnamespacestd;

// To check sentence is palindrome or not

boolsentencePalindrome(string str)

{

intl = 0, h = str.length();

// Lowercase string

for(inti = 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

elseif(!(str[h] >= 'a'&& str[h] <= 'z'))

h--;

// If characters are equal

elseif(str[l] == str[h])

l++, h--;

// If characters are not equal then

// sentence is not palindrome

else

returnfalse;

}

// Returns true if sentence is palindrome

returntrue;

}

// Driver program to test sentencePalindrome()

intmain()

{

string str = "Too hot to hoot.";

if(sentencePalindrome(str))

cout << "Sentence is palindrome.";

else

cout << "Sentence is not palindrome.";

return0;

}

chevron_right

filter_none

Java

filter_none

editclose

play_arrow

linkbrightness_4code

// Java program to find if a sentence is

// palindrome

publicclassGFG

{

// To check sentence is palindrome or not

staticbooleansentencePalindrome(String str)

{

intl = 0;

inth = str.length()-1;

// Lowercase string

str = str.toLowerCase();

// Compares character until they are equal

while(l <= h)

{

chargetAtl = str.charAt(l);

chargetAth = 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

elseif(!(getAth >= 'a'&& getAth <= 'z'))

h--;

// If characters are equal

elseif( getAtl == getAth)

{

l++;

h--;

}

// If characters are not equal then

// sentence is not palindrome

else

returnfalse;

}

// Returns true if sentence is palindrome

returntrue;

}

// Driver program to test sentencePallindrome()

publicstaticvoidmain(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

chevron_right

filter_none

Python

filter_none

editclose

play_arrow

linkbrightness_4code

# Python program to find if a sentence is

# palindrome

# To check sentence is palindrome or not

defsentencePalindrome(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'ands[l] <='z')):

l +=1

# If there is another symbol in right

# of sentence

elif(not(s[h] >='a'ands[h] <='z')):

h -=1

# If characters are equal

elif(s[l] ==s[h]):

l +=1

h -=1

# If characters are not equal then

# sentence is not palindrome

else:

returnFalse

# Returns true if sentence is palindrome

returnTrue

# 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

chevron_right

filter_none

C#

filter_none

editclose

play_arrow

linkbrightness_4code

// C# program to find if a

// sentence is palindrome

usingSystem;

publicclassGFG

{

// To check sentence is

// palindrome or not

staticboolsentencePalindrome(String str)

{

intl = 0;

inth = str.Length - 1;

// Lowercase string

str = str.ToLower();

// Compares character until

// they are equal

while(l <= h)

{

chargetAtl = str[l];

chargetAth = 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

elseif(! (getAth >= 'a'&&

getAth <= 'z'))

h--;

// If characters are equal

elseif( getAtl == getAth)

{

l++;

h--;

}

// If characters are not equal then

// sentence is not palindrome

else

returnfalse;

}

// Returns true if sentence

// is palindrome

returntrue;

}

// Driver Code

publicstaticvoidMain()

{

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.

chevron_right

filter_none

PHP

filter_none

editclose

play_arrow

linkbrightness_4code

<?php

// PHP program to find if a sentence is

// palindrome

// To check sentence is palindrome or not

functionsentencePalindrome($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

elseif(!($str[$h] >= 'a'&& $str[$h] <= 'z'))

$h--;

// If characters are equal

elseif($str[$l] == $str[$h])

{

$l++;

$h--;

}

// If characters are not equal then

// sentence is not palindrome

else

returnfalse;

}

// Returns true if sentence is palindrome

returntrue;

}

// Driver program to test sentencePalindrome()

$str= "Too hot to hoot.";

if(sentencePalindrome($str))

echo"Sentence is palindrome.";

else

echo"Sentence is not palindrome.";

return0;

?>

chevron_right

filter_none

Output:

Sentence is palindrome.

This article is contributed by nuclode. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.