×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: C#
Posted by: Spiro Bad
Added: Feb 19, 2017 1:12 PM
Modified: Feb 19, 2017 1:45 PM
Views: 13
Tags: reverse
Reverse a string or an int. Exmple:
  1. public static string ReverseThis(string s)
  2. {
  3.     char[] arr = s.ToCharArray();
  4.     Array.Reverse(arr);
  5.     return new string(arr);
  6. }
  7.  
  8. public static int ReverseThis(int i)
  9. {
  10.     int r;
  11.     int left = i;
  12.     int rev = 0;
  13.     while (left > 0)
  14.     {
  15.          r = left % 10;
  16.          rev = rev * 10 + r;
  17.          left = left / 10;  //left =
  18.     }
  19.     return rev;
  20. }
  21.  
  22. //Examples:
  23. Console.WriteLine(ReverseThis("Test"));
  24. //Returns: tseT
  25. Console.WriteLine(ReverseThis(12345));
  26. //Returns: 54321