1
// Palindrome
using System;

class Mainclass{
  public static void Main(string[] args){
    string user_input = Console.ReadLine();
    char[] before_reversing = new char[user_input.Length];
    for(int i=0;i<user_input.Length;i++){
      before_reversing[i] = user_input[i];
    }
    char[] after_reversing = Array.Reverse(before_reversing);
    if (before_reversing == after_reversing) {
      Console.WriteLine("It is a palindrome");
    }
    else{
      Console.WriteLine("Not a palindrome");
    }
  }
}

I can't unable run the code due to an error ... can someone help I'm new to programming in C Sharp. And practicing my coding skill and got stuck in palindrome.

Hemanth
  • 27
  • 4
  • 1
    You may want to lookup how [`Array.Reverse`](https://learn.microsoft.com/en-us/dotnet/api/system.array.reverse?view=net-5.0) works, specifically its return type. – gunr2171 Apr 21 '21 at 16:06
  • Does this answer your question? [c# Trying to reverse a list](https://stackoverflow.com/questions/6980608/c-sharp-trying-to-reverse-a-list) (yes this is for Lists but the same concept applies for Arrays) – gunr2171 Apr 21 '21 at 16:07
  • There is no output from method. Method reverses the input array. So you may need to create a temp array. char[] temp = before_reversing; Array.Reverse(temp); – jdweng Apr 21 '21 at 16:12

1 Answers1

1

This is because the Array.Reverse(Array) function is a void and does not have a return in chars[]. For this you can place your after_reversing array like before_reversing and then reverse it. Look:

//Palindrome
using System;

class Mainclass{
  public static void Main(string[] args){
    string user_input = Console.ReadLine();
    char[] before_reversing = new char[user_input.Length];
    for(int i=0;i<user_input.Length;i++){
      before_reversing[i] = user_input[i];
    }
    char[] after_reversing = new char[before_reversing.Length];
    for(int i=0;i<before_reversing.Length;i++){
      after_reversing[i] = before_reversing[i];
    }
    Array.Reverse(after_reversing);
    if (before_reversing == after_reversing) {
      Console.WriteLine("It is a palindrome");
    }
    else{
      Console.WriteLine("Not a palindrome");
    }
  }
}
Poke Clash
  • 106
  • 1
  • 4