0

My Application Exits when i parse a string to int.. I can't solve the problem as I am new to C# in-fact I am new to Programming, Here is my code so far:

public string Reverse(string str)
{
  int num = int.Parse(str);
  int reverse = 0;
  while(num > 0)
  {
    reverse *= 10;
    reverse += num % 10;
    num /= 10;
  }
  return (reverse.ToString());
}

I don't want it to quit the Application

Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42

1 Answers1

3

Use the TryParse pattern instead:

int parsedNumber;
var success = int.TryParse("1", out parsedNumber);

On another note, a much better Reverse algorithm is to do:

public static string Reverse(string s)
{
    char[] charArray = s.ToCharArray();
    Array.Reverse(charArray);
    return new string(charArray);
}
MaYaN
  • 6,683
  • 12
  • 57
  • 109