Skip to main content
edited body
Source Link
Bruno Costa
  • 5.6k
  • 20
  • 42

I strongly agree with lol.upvote answer, regarding the separation of concerns. I Think you should have a method to get the chars and then present them to the user, because you may need know what chars of a number are later. And so I made all the major approaches that you may have when implementing a GetDigits. A iterative, recursive and a straightforward way:

public static char[] GetDigitsRecursive(int value){
    int length = value.ToString().Length;
    return GetDigitsRecursiveAux(value, new char[length], length-1);
}

private static char[] GetDigitsRecursiveAux(int value, char[] digits, int idx){
    digits[idx] = (char)(value % 10 + '0');
    if(value ==<= 09){
        return digits;
    }
    return GetDigitsRecursiveAux(value/10, digits, idx-1);
}

public static char[] GetDigitsIterative(int value){
    char[] digits = new char[value.ToString().Length];
    for(int i = digits.Length-1; i >= 0; --i){
        digits[i] = (char)(value %10 + '0');
        value = value / 10;
    }
    return digits;
}

public static char[] GetDigitsSimplistic(int value){
    return value.ToString().ToCharArray();
}

I strongly agree with lol.upvote answer, regarding the separation of concerns. I Think you should have a method to get the chars and then present them to the user, because you may need know what chars of a number are later. And so I made all the major approaches that you may have when implementing a GetDigits. A iterative, recursive and a straightforward way:

public static char[] GetDigitsRecursive(int value){
    int length = value.ToString().Length;
    return GetDigitsRecursiveAux(value, new char[length], length-1);
}

private static char[] GetDigitsRecursiveAux(int value, char[] digits, int idx){
    digits[idx] = (char)(value % 10 + '0');
    if(value == 0){
        return digits;
    }
    return GetDigitsRecursiveAux(value/10, digits, idx-1);
}

public static char[] GetDigitsIterative(int value){
    char[] digits = new char[value.ToString().Length];
    for(int i = digits.Length-1; i >= 0; --i){
        digits[i] = (char)(value %10 + '0');
        value = value / 10;
    }
    return digits;
}

public static char[] GetDigitsSimplistic(int value){
    return value.ToString().ToCharArray();
}

I strongly agree with lol.upvote answer, regarding the separation of concerns. I Think you should have a method to get the chars and then present them to the user, because you may need know what chars of a number are later. And so I made all the major approaches that you may have when implementing a GetDigits. A iterative, recursive and a straightforward way:

public static char[] GetDigitsRecursive(int value){
    int length = value.ToString().Length;
    return GetDigitsRecursiveAux(value, new char[length], length-1);
}

private static char[] GetDigitsRecursiveAux(int value, char[] digits, int idx){
    digits[idx] = (char)(value % 10 + '0');
    if(value <= 9){
        return digits;
    }
    return GetDigitsRecursiveAux(value/10, digits, idx-1);
}

public static char[] GetDigitsIterative(int value){
    char[] digits = new char[value.ToString().Length];
    for(int i = digits.Length-1; i >= 0; --i){
        digits[i] = (char)(value %10 + '0');
        value = value / 10;
    }
    return digits;
}

public static char[] GetDigitsSimplistic(int value){
    return value.ToString().ToCharArray();
}
Source Link
Bruno Costa
  • 5.6k
  • 20
  • 42

I strongly agree with lol.upvote answer, regarding the separation of concerns. I Think you should have a method to get the chars and then present them to the user, because you may need know what chars of a number are later. And so I made all the major approaches that you may have when implementing a GetDigits. A iterative, recursive and a straightforward way:

public static char[] GetDigitsRecursive(int value){
    int length = value.ToString().Length;
    return GetDigitsRecursiveAux(value, new char[length], length-1);
}

private static char[] GetDigitsRecursiveAux(int value, char[] digits, int idx){
    digits[idx] = (char)(value % 10 + '0');
    if(value == 0){
        return digits;
    }
    return GetDigitsRecursiveAux(value/10, digits, idx-1);
}

public static char[] GetDigitsIterative(int value){
    char[] digits = new char[value.ToString().Length];
    for(int i = digits.Length-1; i >= 0; --i){
        digits[i] = (char)(value %10 + '0');
        value = value / 10;
    }
    return digits;
}

public static char[] GetDigitsSimplistic(int value){
    return value.ToString().ToCharArray();
}