Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm not sure how to check individual strings to depict if they're palindromes or not. The program treats both strings as one therefore displaying 'it is not a palindrome' when one of the strings actually is.

What I have tried:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Palindrome
{
    class Program
    {
        static void Main(string[] args)
        {
            Stack list = new Stack();
            string ch;
            string value1= "eye";
            string value2 = "view";

            string word = string.Format("{0}\n{1}\n", value1, value2);                                                              
            bool palindrome = true;

            for (int x = 0; x < word.Length; x++)
                list.push(word.Substring(x, 1));
            int pos = 0;

            while (list.count > 0)
            {
                ch = list.pop().ToString();
                if (ch != word.Substring(pos, 1))
                {
                    palindrome = false;
                    break;
                }
                pos++;
            }
            if (palindrome)
                Console.WriteLine(word + " \nis a palindrome.");
            else
                Console.WriteLine(word + " \nis not a palindrome.");
            Console.Read();
        }

        class Stack
        {
            private int index;
            private ArrayList list;
            public Stack()
            {
                list = new ArrayList();
                index = -1;
            }
            public int count
            {
                get
                {
                    return list.Count;
                }
            }
            public void push(object item)
            {
                list.Add(item);
                index++;
            }
            public object pop()
            {
                object obj = list[index];
                list.RemoveAt(index);
                index--;
                return obj;
            }
            public void clear()
            {
                list.Clear();
                index = -1;
            }
            public object peek()
            {
                return list[index];
            }
      
        }
    }
}
Posted
Updated 8-May-17 1:15am

You are actually checking the value of word to see if it is a palindrome.
eye
view
is not a palindrome. If you want to check those individual words then you have to put in some processing to separate the words out e.g split by \n and handle each result in that array in a loop.

If you change your code to
string word = value1;
then it correctly notes this is a palindrome.

Perhaps the most famous palindrome is "Madam, I'm Adam". Your code will not view this as a palindrome because:
1. You are not removing or handling punctuation marks such as ', , etc
2. You are not handling differences in case M should be "equal" to m for the purposes of finding a palindrome.
3. You are inserting \n into your test data.
 
Share this answer
 
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
Comments
OfficialSub0 7-May-17 21:53pm    
I used debugger and I am not sure, that's why I created this post...
Patrice T 7-May-17 22:42pm    
With debugger, inspect variables and look at what you do with the two words.
Member 10317286 7-May-17 22:44pm    
Why not try doing each word separately and see what you get?

Try using linq. Something like


C#
var names=new List<string>(){"level","eve","tub","Eye","Racecar","motor"};
var palindromes = names.Select(n => n.ToLower().ToCharArray())
.Where(n => n.Reverse().SequenceEqual(n));
foreach (var p in palindromes)
{
 Console.WriteLine(p);
}
 
Share this answer
 
v2
Comments
F-ES Sitecore 8-May-17 7:13am    
Your code won't identify that "Race car" is a palindrome :)
George Swan 8-May-17 9:31am    
Thanks for the humorous reply. I was expecting a reprimand for responding to the question. Most don't realise just how useful Linq can be so I thought I'd post it up as an example.
F-ES Sitecore 8-May-17 9:35am    
This is clearly homework though, so the solution will have to cover things they've learned in their class so far so LINQ probably won't be considered a valid solution.

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