-2

I made a program which is checking if a word is a palindrome.

But if I type "Lol" it says that's palindrome.

I think palindrome must be completly same. ?

 public static bool IsPalindrome(string text)
        {
            int min = 0;
            int max = text.Length - 1;

            while (true)
            {
                if (min > max)
                {
                    return true;
                }

                char a = text[min];
                char b = text[max];

                if (char.ToLower(a) != char.ToLower(b))
                {
                    return false;
                }

                min++;
                max--;
            }
        }

Input: "Lol" Output: "Lol is NOT a palindrome"

Clemens
  • 123,504
  • 12
  • 155
  • 268
  • Well you do check whether `char.ToLower(a) != char.ToLower(b)`, so obviously it's case-insensitive. What's your actual question? – canton7 Jul 04 '19 at 12:05
  • How to make it case-sensitive –  Jul 04 '19 at 12:05
  • 1
    Remove the call to `ToLower`. – mm8 Jul 04 '19 at 12:06
  • 1
    I showed you the code which makes it case-insensitive. To make it case-insensitive, change that bit of code so it is not case-insensitive... I really don't understand where you're coming from - you said you wrote this code, and you explicitly went out of your way to add logic to make it case-insensitive. Now you're asking how to change this... – canton7 Jul 04 '19 at 12:06
  • @canton7 Perhaps he did write the code but did not understand what the `ToLower()` function does when looking on the internet. – Cleptus Jul 04 '19 at 12:13
  • @bradbury9 I don't understand why he added it if he had no understanding of it. I think it's probably more likely he lied about writing the code... – canton7 Jul 04 '19 at 12:16
  • If he is not english or does not understand english, perhaps copied the character comparision without noticing it did not do what he expected to do. Kinda weird, I agree, but not impossible – Cleptus Jul 04 '19 at 12:19
  • @canton7 If I wanted to copy the could I could do it [https://stackoverflow.com/questions/9790749/check-if-a-string-is-a-palindrome](https://stackoverflow.com/questions/9790749/check-if-a-string-is-a-palindrome) here easily –  Jul 04 '19 at 12:24

1 Answers1

0

To make your method case-sensitive, you should remove the calls to ToLower():

while (true)
{
    if (min > max)
    {
        return true;
    }

    char a = text[min];
    char b = text[max];

    if (a != b)
    {
        return false;
    }

    min++;
    max--;
}

The "Lol" won't be considered as a palindrome but "LoL" will.

Cleptus
  • 3,446
  • 4
  • 28
  • 34
mm8
  • 163,881
  • 10
  • 57
  • 88
  • Thank you, I can't tick the answer yet. –  Jul 04 '19 at 12:13
  • I would encourage checking the [char.Tower() documentation](https://learn.microsoft.com/en-us/dotnet/api/system.char.tolower?view=netframework-4.7.2#System_Char_ToLower_System_Char_) to fully understand the reason. – Cleptus Jul 04 '19 at 12:16