Untitled

mail@pastecode.io avatar
unknown
csharp
3 years ago
767 B
3
Indexable
Never
public int Reverse(int x) {
        var y = x.ToString();
            var len = y.Length;
            bool isNegative = false;
            if(y[0] == '-')
            {
                isNegative = true;
                y = y.Substring(1);
                len--;
            }
            
            char[] vs = new char[len];
            for (int i = len - 1; i >= 0; i--)
            {
                vs[(len - 1) - i] = y[i];

            }

            if (Int32.TryParse(vs, out int result))
            {
                if (isNegative)
                {
                    result *= -1;
                }
                return result;
            }
            else
            {
                return 0;
            }
    }