Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

struggling to debug code problem

$
0
0
hi all

so here is my challeneg to debug this code i will first give u what i was given then i will show what i managed to do and what error i get

public class Pal {

    public static void Main() {
        Console.WriteLine("How many chars? ");
        int size =int.Parse(Console.ReadLine());
            char [] a = new char[size];
        for (int i = 0; i < a.Length; i++) {
            Console.WriteLine(" Enter Next Character");
            
                a[i] = int.Parse(Console.ReadLine());
        }
        Console.Write(" the word is ");
        for (int i = 0; i < a.Length; i++)
            
            Console.Write(a[i]);

        for (int i = 1;  i < a.Length/2; i++)
            if (a[i] != a[a.Length-i]){
                Console.WriteLine(" is not a palidrome");
                Environment.Exit(0);
            }
        Console.WriteLine(" is a palindrome");
        Console.ReadLine();
    }
}




here is what i managed to get to

public class Pal {

    public static void Main() {
        Console.WriteLine("How many chars? ");
        int size =int.Parse(Console.ReadLine());
            char [] a = new char[size];
        for (int i = 0; i < a.Length; i++) {
            Console.WriteLine(" Enter Next Character");
            
                a[i] = char.Parse(Console.ReadLine());
        }
        Console.Write(" the word is ");
        for (int i = 0; i < a.Length; i++)
            
            Console.Write(a[i]);

        for (int i = 1;  i < a.Length; i++)
            if (a[i] != a[a.Length-i]){
                Console.WriteLine(" is not a palidrome");
                Environment.Exit(0);
            }
        Console.WriteLine(" is a palindrome");
        Console.ReadLine();
    }
}



now i am using the example with this as 4 letters o,t,t,o hoping for it to work but i am getting it display "IS not a palindrome" when it should be?

thanks

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>