0

I am writing code to check for symmetry within a string. I convert to a Char array and then I would like to use the Array.Reverse method. I keep getting a cannot convert type void to Char[]. Any insight would be greatly appreciated my code is as follows.

namespace ConsoleApp1
{
  class Program
  {
    static void Main(string[] args)
    {
        checkPalindrome("aabaa");
    }
   public static bool checkPalindrome(string inputString)
    { 
        char[] charArray = inputString.ToCharArray();
        char[] reverseArray = Array.Reverse(charArray);
        Array.Reverse(charArray);

    //Console.Write(charArray);
    //Console.Write(reverseArray);

    if (charArray = Array.Reverse(charArray))
    {
        return true;
    }
    else
    {
        return false;
    }
  }

  }
}
6
  • 1
    @huck_cussler Array.Reverse returns void, so neither the assignment nor the comparison will work. Commented May 8, 2018 at 15:16
  • @Amy Correct. I will edit my comment to note that it is not relevant to OP's actual question. Thanks. Commented May 8, 2018 at 15:17
  • Array.Reverse is an in place reverse, meaning it mutates the array. Commented May 8, 2018 at 15:17
  • @huck_cussler Even it if did return an array comparing them would be a reference equality check, not a check that each item in both arrays is equal. Commented May 8, 2018 at 15:19
  • 1
    @huck_cussler But Delete never expires ;) Commented May 8, 2018 at 15:24

3 Answers 3

8

Your problem is here:

char[] reverseArray = Array.Reverse(charArray);

Array.Reverse changes the contents of the array that you pass it. It doesn't return any value.

You can use LINQ to do something like this instead:

char[] reverseArray = charArray.Reverse().ToArray();

Then, when you're checking for equality, you need to do more than the default object-equals type of equality check:

if (charArray.SequenceEqual(reverseArray))

Also, .ToArray() is only necessary if you need an array, and you don't need one for SequenceEqual(). And the if(something) {return true;} else {return false;} pattern can be simplified to return something;

So the entire method can actually be written in a single line:

public static bool checkPalindrome(string inputString)
{ 
    return inputString.Reverse().SequenceEqual(inputString);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I truly appreciate your solution to my entire algorithim I will use your code in my final product. I am still confused as to why Array.Reverse(genericArray) cannot be applied to a simple if statement I still encounter the same char [] cannot be compared to void.
@AndrewMattick: An if statement has to take a boolean expression, and your boolean expression is a "binary expression" (meaning it has two parts) around the == operator. The left-hand side is a char[], but the right-hand side is a call to a method that doesn't return anything (i.e. it's a "void" method). Calling Array.Reverse causes a side-effect on the parameter you give it, but since no value is returned, the method call can't be used in a context that needs to have some kind of value.
0

Here's a better way to determine symetry in a string.

public bool IsSymetric(string str)
{
    if(str == null) return true; // or false or throw an exception

    for(int i = 0; i < str.Length/2; i++)
    {
        if(str[i] != str[str.Length - 1 - i])
        {
            return false;
        }
    }

    return true;
}

Comments

0

A better way - no array cloning/array extraction:

    static bool checkPalindrome(string inputString)
    {
        var length = inputString.Length;
        var half = length / 2 + 1;
        return length <= 1 || inputString.Take(half).SequenceEqual(inputString.Reverse().Take(half));
    }

2 Comments

why do you call the extension method in such a weird way
To kinda emphasize the symmetry or the compared objects, but you're right - I changed it now. And optimized a little to check only the half.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.