2

i would like your help with my program. I'm trying to check if the user input is a palindrome. This is my code.

        Stack cstack = new Stack();
        Stack rstack = new Stack();
        Console.WriteLine("Enter a palindrome string");
        string input = Console.ReadLine();

        foreach (char c in input)
        {
            cstack.Push(c);
        }

        foreach (var v in cstack)
        {
            rstack.Push(v);
        }

        if (cstack.Equals(rstack))
        {
            Console.Write("String is a palindrome");
        }
        else
        {
            Console.Write("String is not a palindrome");
        }

        Console.ReadLine();

So what i hope my code is doing is getting input from the user. Pushing the user input as characters into the cstack using a foreach loop. Reversing the characters in the cstack (Which should hold the users input) into a new stack called rstack using another foreach loop. Then using an if else statement which will check to see if the c stack which holds the original input is equal to the reverse stack. This comparison will determine if the string is a palindrome.

Cheers everyone for the help. I'm glad i didn't come out as an idiot with no clue as to what he was doing.

Sorry guys but would anyone know how to make it so the palindrome ignores case sensitivity. I assume you convert the string into lowercase with the ToLower, but really i have no idea I've never needed a string to be case insensitive before.

nat1
  • 23
  • 4

2 Answers2

1
Stack<char> cstack = new Stack<char>();
            string input = "PoP";

            var inputToUpper = input.ToUpper(); /*assuming case senstivity is not to be considered */

            foreach (char c in inputToUpper)
            {
                cstack.Push(c);
            }

            bool isPalindrome = true;
            var noOfItems = cstack.Count;

            for(int i=0; i< noOfItems ; i++)
            {
                if (inputToUpper[i] != cstack.Pop())
                {
                    isPalindrome = false; break;
                }
            }

            if (isPalindrome)
            {
                Console.WriteLine("Palindrome");
            }
            else
            {
                Console.WriteLine("Non Palindrome");
            }

You can refer live demo here

rahulaga-msft
  • 3,964
  • 6
  • 26
  • 44
  • Thank you for the help. I'm sorry my requirement forced you to edit your code. – nat1 Mar 24 '18 at 06:48
  • A more efficient solution would be to use a for loop, and then compare the character at each part of the string to the popped value. Then you can exit early if it isn't a palindrome. – ProgrammingLlama Mar 24 '18 at 07:20
0

This palindrome solution works for palindrome sentences with spaces

private static bool isPalinDrome(string word)
    {
        word = word.ToLower();
        word = word.Replace(" ", "");
        char[] wordArray = word.ToCharArray();
        String reverseWord = "";

        for (int i = wordArray.Length - 1; i >= 0; i--)
        {
            reverseWord = reverseWord + wordArray[i];
        }

        if (word == reverseWord)
        {
            return true;
        }

        return false;
    }
Gabe
  • 136
  • 2
  • 11