0

What is the most effective code to convert int to string without using a native convert function.

 public static void Main(string[] args)
    {
        string y = Convert(990);
        Console.WriteLine(y);
        Console.ReadLine(); 
    }


    public static string Convert(int x)
    {

         char[] Str = new char[20];
         int i = 0;
         while (x != 0)
         {
             Str[i++] = x % 10 + '0';
             x = x / 10;
         }

         return Str.ToString();// How do I handle this without the native function?
    }

The above doesnt seem to work. Please advise.

4
  • 2
    what? what do you wnat that for? Commented Jul 4, 2013 at 21:21
  • This is a school assignment, and therefore not for us to do. Commented Jul 4, 2013 at 21:21
  • 2
    What can you use and what can't you use? Commented Jul 4, 2013 at 21:26
  • 1
    While an obvious classroom or interview question, at least you posted code. Plus it gave me an excuse to write 'clever' code. Fun occasionally. Commented Jul 4, 2013 at 22:11

3 Answers 3

1

Here's a solution without a String constructor or a .ToString() call. This one handles negative numbers. It's a bit too 'clever' for my taste, but it's an academic exercise anyway...

void Main()
{
    Console.WriteLine(Convert(-5432));
}

String Convert(int i)
{
    return String.Join("",Digits(i).Reverse());
}

IEnumerable<char> Digits(int i)
{
    bool neg = false;
    if(i==0) {yield return '0'; yield break;}
    if(i<0) { i = -i; neg = true;}
    while (i!=0)
    {
        char digit = (char)(i % 10 + '0');
        i = i / 10;
        yield return digit;
    }
    if(neg) yield return '-';

    yield break;
}
Sign up to request clarification or add additional context in comments.

Comments

1

It might be easier to construct your result using a StringBuilder rather than a char array:

public static string Convert(int x)
{
     StringBuilder sb = new StringBuilder();

     while (x != 0)
     {
         sb.Insert(0, (char)(x % 10 + '0'));
         x = x / 10;
     }

     return sb.ToString();
}

The Insert(0, x) allows you to insert the more significant digits at the front.

2 Comments

What do you think x % 10 + '0' is?
@DavidHeffernan: You're right; I was assuming that that part of the OP's code was correct. Now fixed.
1

You are adding the least significant digits to the start of the string. You'll need to reverse the string, or add the digits in reverse order, or insert them rather than append them.

And you'll need to do some casting. For example, here's one way to do it:

Str[i++] = (char)(x % 10 + '0');

Finally, you cannot use ToString() like that. You want this:

return new string(Str, 0, i);

Although StringBuilder might be more suitable.

And note that your code won't work properly for negative input values, or for zero.

So, here's a version that handles all of that:

public static string Convert(int x)
{
    if (x == 0)
    {
        return "0";
    }
    StringBuilder sb = new StringBuilder();
    string prefix = "";
    if (x < 0)
    {
        prefix = "-";
        x = -x;
    }
    while (x != 0)
    {
        sb.Insert(0, (char)(x % 10 + '0'));
        x = x / 10;
    }
    return prefix + sb.ToString();
}

This is pretty ugly though!

Comments

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.