-1

I'm currently wondering if when a given string (word), how can I determine if it is a palindrome or not. A palindrome is a word or phrase that is the same if read forward or backward. I think I can solve this by looping through the half the word and comparing each letter with the other half. An example for this could be: (word[0] == word[word.Length-1-0]) would compare the first letter with the last letter of word, and (word[1] == word[word.Length-1-1]) would compare the second letter with the second to last letter.

  • Example Input could be: racecar

  • Example Output: True

Am I approaching this problem correctly towards the proper solution?

Here's a bit of I've written down so far.

public bool Test6(string word)
        {
            for (int i = 0; i < word.Length; i++)
            {
                if (word[0] == word[word.Length - 1 - 0])
                {

                }
Priya
  • 1,359
  • 6
  • 21
  • 41
Zed
  • 23
  • 1
  • 2
  • http://www.dotnetperls.com/palindrome – Eldho Jun 09 '16 at 05:24
  • Getting there - this is the right approach. An observation: The loop counter only needs to go for half the string length (think about why this is so). – Matthew Watson Jun 09 '16 at 05:56
  • That linked duplicate question... the marked answer... Oh my. My suggestion: Continue with your own approach and DO NOT USE the answer in the linked question! It's not really an answer to this question. – Matthew Watson Jun 09 '16 at 05:58
  • I can't write an answer to this, so I'll have to put this in a comment (and you'll have to reformat this to be readble): `public static bool IsPalindrome(string s){ for (int i = 0, j = s.Length-1; i < j; ++i, --j) if (s[i] != s[j]) return false; return true; }` - [Or try the code here](https://dotnetfiddle.net/gZHf3h) – Matthew Watson Jun 09 '16 at 07:34

5 Answers5

4

I would do this (quickly).

string input = "..."
string reverse =  new string(input.ToCharArray().Reverse().ToArray());

if(input.Equals(reverse)
{
   // polindrome.
}
Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
1

Please follow this link http://www.dotnetperls.com/palindrome

You can do it using this example without using any built in method :

using System;

class Program
{
    public static bool IsPalindrome(string value)
    {
    int min = 0;
    int max = value.Length - 1;
    while (true)
    {
        if (min > max)
        {
        return true;
        }
        char a = value[min];
        char b = value[max];
        if (char.ToLower(a) != char.ToLower(b))
        {
        return false;
        }
        min++;
        max--;
    }
    }

    static void Main()
    {
    string[] array =
    {
        "civic",
        "deified",
        "deleveled",
        "devoved",
        "dewed",
        "Hannah",
        "kayak",
        "level",
        "madam",
        "racecar",
        "radar",
        "redder",
        "refer",
        "repaper",
        "reviver",
        "rotator",
        "rotor",
        "sagas",
        "solos",
        "sexes",
        "stats",
        "tenet",

        "Dot",
        "Net",
        "Perls",
        "Is",
        "Not",
        "A",
        "Palindrome",
        ""
    };

    foreach (string value in array)
    {
        Console.WriteLine("{0} = {1}", value, IsPalindrome(value));
    }
    }
}
Alina Anjum
  • 1,178
  • 6
  • 30
  • 53
  • 1
    This answer is arguably borderline plagiarism of Sam Allen's code which is **copyright 2007-2016**. I don't think the link makes it clear you are citing a foreign source –  Jun 09 '16 at 05:32
  • @MickyD i added the link after I found exact answer on that site – Anonymous Duck Jun 09 '16 at 05:37
  • Other than that it is too verbose compared to Hari's [fine answer](http://stackoverflow.com/a/37717473/585968) –  Jun 09 '16 at 05:37
  • @MickyD That is not a fine answer. It's horribly inefficient and makes several needless copies of the string. The OP is already well on the way to a good correct answer. – Matthew Watson Jun 09 '16 at 06:00
  • @MatthewWatson Meh. [This answer](http://stackoverflow.com/a/9792684/585968) and [this one](http://stackoverflow.com/a/9791036/585968) from the linked duplicated question would appear to disagree with you public opinion-wise –  Jun 09 '16 at 06:10
  • @MickyD I wouldn't always rely on the court of Public Opinion to give the best answers. :) It should be obvious that if all you need to do is to iterate over half the string comparing characters without making any copies of the string, then a solution which makes THREE copies of the string isn't very good. – Matthew Watson Jun 09 '16 at 07:29
  • @MatthewWatson good point my friend :) –  Jun 09 '16 at 07:38
1

A shorter version using LINQ would be

bool IsPalindrome(string x)
{
   return Enumerable.Range(0,x.Length/2).All(e => x[e] == x[x.Length-1-e]);
}
Alastair Brown
  • 1,598
  • 8
  • 12
1

Please find the code below

using System;
using System.Linq;

class MyClass
{
    static void Main(string[] args) {
        string str = Console.ReadLine();

        string backwardsGuy = new string(str.Reverse().ToArray());
        if(str==backwardsGuy)
        {
            Console.WriteLine("True");
        }
        else
        {
            Console.WriteLine("False");
        }
    }
}
Coder
  • 406
  • 9
  • 22
0

Sample code-

static void Main(string[] args)
        {
            string s,revs="";
            Console.WriteLine(" Enter string");
            s = Console.ReadLine();
            for (int i = s.Length-1; i >=0; i--) //String Reverse
            {
                revs += s[i].ToString();
            }
            if (revs == s) // Checking whether string is palindrome or not
            {
                Console.WriteLine("String is Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
            }
            else
            {
                Console.WriteLine("String is not Palindrome \n Entered String Was {0} and reverse string is {1}", s, revs);
            }
            Console.ReadKey();
        }
Sanket
  • 19,295
  • 10
  • 71
  • 82