Home   Cover Cover Cover Cover
 

StringBuilder

/csbook/solutions/03/A09.cs
using System;
using System.Text;

class Test {
  
  static string Convert(int n) {
    StringBuilder b = new StringBuilder();
    if (n < 0) { b.Append('-'); n = -n; }
    int[] a = new int[10];
    int i = 0;
    do { a[i] = n % 10; n = n / 10; i++; } while (n != 0);
    do { i--; b.Append((char)(a[i] + '0')); } while (i > 0);
    return b.ToString();
  }
  
  public static void Main() {
    Console.WriteLine(Convert(0));
    Console.WriteLine(Convert(123));
    Console.WriteLine(Convert(-512));
  }
}