Click here to Skip to main content
15,912,507 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a function in C# that checks if a given sentence is a palindrome. A palindrome is a word, phrase, verse, or sentence that reads the same backward or forward. Only the order of English alphabet letters (A-Z and a-z) should be considered, other characters should be ignored.

For example, IsPalindrome("Noel sees Leon.") should return true as spaces, period, and case should be ignored resulting with "noelseesleon" which is a palindrome since it reads same backward and forward.

What I have tried:

C#
using System;

public class Palindrome
{
public static bool IsPalindrome(string str)
{
string rev = "";
//string [] words
  
for(int i = input1.Length-1; i >= 0; i--)
       {
   rev += input1[i];
}
  
if(str.ToLower() == rev.ToLower())
{
return true;
  
}else{
return false;
}
}

public static void Main(string[] args)
{
Console.WriteLine(IsPalindrome("Noel sees Leon."));
}
}
Posted
Updated 11-Jun-16 17:19pm
v2
Comments
Garth J Lancaster 23-Mar-16 3:53am    
so what is your question / issue ?
FARONO 23-Mar-16 4:37am    
In this code you don't ignore spaces and other characters..? The period at the end of you call ("Noel sees Leon.") ruïns the palingdrome

[edit]sorry, Solution 2 fixed it
BillWoodruff 23-Mar-16 6:10am    
Why have you not tried removing the white space and punctuation in your example code ?
Philippe Mori 23-Mar-16 12:38pm    
You should do your homework by yourself. Otherwise, you will never become a good programmer...

Tons of answers/solutions/tips/articles you'll find here: Search - CodeProject[^]
 
Share this answer
 
v2
Comments
BillWoodruff 23-Mar-16 6:11am    
Is this really a "solution" ?
Maciej Los 23-Mar-16 6:17am    
Hi Bill,
Yes, this is the best solution. Question about palindrome has been quite often asked. OP needs to improve already existing solutions to own logic.
Try this

C#
class Program
  {
      static void Main(string[] args)
      {
          Console.WriteLine(IsPalindrome("Noel sees Leon."));
          Console.ReadLine();
      }

      public static bool IsPalindrome(string str)
      {
          string forward = "";
          foreach (char c in str.ToLower())  // ToLower to ignore the case
              if (char.IsLetter(c))  // ignore the period, space and other char except alphabets
                  forward += c;

          char[] temp = forward.ToCharArray();
          Array.Reverse(temp);
          string reverese = new string(temp);
          return forward == reverese;
      }
  }
 
Share this answer
 
Comments
F-ES Sitecore 23-Mar-16 5:52am    
You shouldn't do other people's homework, if people are stuck then they should see their tutors. Giving people answers doesn't help anyone.
Karthik_Mahalingam 23-Mar-16 6:14am    
ok

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900