How to convert given number to a character array
Given an integer number N, the task is to convert it into a character array.
Example:
Input: N = 2020
Output: {2, 0, 2, 0}
Explanation: Here char array arr[] = {2, 0, 2, 0}
Input: N = 12349
Output: {1, 2, 3, 4, 9}
Explanation: Here char array arr[] = {1, 2, 3, 4, 9}
Approach 1: The basic approach to do this, is to recursively find all the digits of N, and insert it into the required character array.
- Count total digits in the number.
- Declare a char array of size digits in the number.
- Separating integer into digits and accommodate it to a character array.
- Add ASCII value of character ‘0’ is 48 in each element of the array.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <cstring>
using namespace std;
char * convertIntegerToChar( int N)
{
int m = N;
int digit = 0;
while (m > 0) {
digit++;
m /= 10;
}
char * arr = new char [digit];
char arr1[digit + 1];
int index = 0;
while (N > 0) {
index++;
arr1[index] = static_cast < char >(N % 10 + 48);
N /= 10;
}
for ( int i = 0; i < index; i++) {
arr[i] = arr1[index - i];
}
arr[index] = '\0' ;
return arr;
}
int main()
{
int N = 12349;
int len = 5;
char * arr = convertIntegerToChar(N);
for ( int i = 0; i < len; i++)
cout << arr[i] << " " ;
delete [] arr;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
char * convertIntegerToChar( int N)
{
int m = N;
int digit = 0;
while (m) {
digit++;
m /= 10;
}
char * arr;
char arr1[digit];
arr = ( char *) malloc (digit);
int index = 0;
while (N) {
arr1[++index] = N % 10 + '0' ;
N /= 10;
}
int i;
for (i = 0; i < index; i++) {
arr[i] = arr1[index - i];
}
arr[i] = '\0' ;
return ( char *)arr;
}
int main()
{
int N = 12349;
int len = 5;
char * arr = convertIntegerToChar(N);
for ( int i = 0; i < len; i++)
printf ( "%c, " , arr[i]);
return 0;
}
|
Java
class GFG{
static char [] convertIntegerToChar( int N)
{
int m = N;
int digit = 0 ;
while (m > 0 )
{
digit++;
m /= 10 ;
}
char [] arr;
char []arr1 = new char [digit + 1 ];
arr = new char [digit];
int index = 0 ;
while (N > 0 )
{
arr1[++index] = ( char )(N % 10 + '0' );
N /= 10 ;
}
int i;
for (i = 0 ; i < index; i++)
{
arr[i] = arr1[index - i];
}
return ( char [])arr;
}
public static void main(String[] args)
{
int N = 12349 ;
int len = 5 ;
char [] arr = convertIntegerToChar(N);
for ( int i = 0 ; i < len; i++)
System.out.printf( "%c, " , arr[i]);
}
}
|
Python3
def convertIntegerToChar(N):
m = N;
digit = 0 ;
while (m > 0 ):
digit + = 1 ;
m / = 10 ;
arr = [ '0' for i in range (digit)]
arr1 = [ '0' for i in range (digit + 1 )];
index = 0 ;
while (N > 0 ):
index + = 1 ;
arr1[index] = chr ( int (N % 10 + 48 ));
N = N / / 10 ;
for i in range ( 0 , index):
arr[i] = arr1[index - i];
return arr;
if __name__ = = '__main__' :
N = 12349 ;
len = 5 ;
arr = convertIntegerToChar(N);
for i in range ( 0 , len , 1 ):
print (arr[i], end = " " );
|
C#
using System;
class GFG{
static char [] convertintToChar( int N)
{
int m = N;
int digit = 0;
while (m > 0)
{
digit++;
m /= 10;
}
char [] arr;
char []arr1 = new char [digit + 1];
arr = new char [digit];
int index = 0;
while (N > 0)
{
arr1[++index] = ( char )(N % 10 + '0' );
N /= 10;
}
int i;
for (i = 0; i < index; i++)
{
arr[i] = arr1[index - i];
}
return ( char [])arr;
}
public static void Main(String[] args)
{
int N = 12349;
int len = 5;
char [] arr = convertintToChar(N);
for ( int i = 0; i < len; i++)
Console.Write( "{0}, " , arr[i]);
}
}
|
Javascript
<script>
function convertIntegerToChar(N)
{
let m = N;
let digit = 0;
while (m > 0)
{
digit++;
m = parseInt(m / 10, 10);
}
let arr;
let arr1 = new Array(digit + 1);
arr = new Array(digit);
let index = 0;
while (N > 0)
{
arr1[++index] = String.fromCharCode(N % 10 + '0' .charCodeAt());
N = parseInt(N / 10, 10);
}
let i;
for (i = 0; i < index; i++)
{
arr[i] = arr1[index - i];
}
return arr;
}
let N = 12349;
let len = 5;
let arr = convertIntegerToChar(N);
for (let i = 0; i < len; i++)
document.write(arr[i] + ", " );
</script>
|
Time Complexity: O(log N), where N is the input integer.
Space Complexity: O(digit), where the digit is the number of digits in the input integer.
Approach 2: We can further simplify above approach using dynamic array (vector in C++, ArrayList in Java and so on…). We will create a dynamic array and insert new digit at the beginning of this array so we will not have to calculate size of it separately and also we will not have to reverse the array. See the below implementation.
C++
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
vector< char > convertIntegerToChar( int N)
{
vector< char > arr;
while (N != 0) {
arr.insert(arr.begin(), N % 10 + '0' );
N /= 10;
}
return arr;
}
int main()
{
int N = 12349;
vector< char > arr = convertIntegerToChar(N);
for ( auto & it : arr)
cout << it << ", " ;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.List;
public class IntegerToCharArray {
public static List<Character> convertIntegerToChar( int N) {
List<Character> arr = new ArrayList<>();
while (N != 0 ) {
arr.add( 0 , ( char ) (N % 10 + '0' ));
N /= 10 ;
}
return arr;
}
public static void main(String[] args) {
int N = 12349 ;
List<Character> arr = convertIntegerToChar(N);
for ( char ch : arr) {
System.out.print(ch + ", " );
}
}
}
|
Python3
def convertIntegerToChar(N):
arr = []
while N ! = 0 :
arr.insert( 0 , chr ((N % 10 ) + ord ( '0' )))
N / / = 10
return arr
if __name__ = = '__main__' :
N = 12349
arr = convertIntegerToChar(N)
for ch in arr:
print (ch, end = ', ' )
|
C#
using System;
using System.Collections.Generic;
public class Program {
public static List< char > ConvertIntegerToChar( int N)
{
List< char > arr = new List< char >();
while (N != 0) {
arr.Insert(0, ( char )(N % 10 + '0' ));
N /= 10;
}
return arr;
}
public static void Main()
{
int N = 12349;
List< char > arr = ConvertIntegerToChar(N);
foreach ( char c in arr) { Console.Write(c + ", " ); }
}
}
|
Javascript
function convertIntegerToChar(N)
{
let arr = [];
while (N !== 0)
{
arr.unshift(String.fromCharCode(N % 10 + '0' .charCodeAt()));
N = Math.floor(N / 10);
}
return arr;
}
let N = 12349;
let arr = convertIntegerToChar(N);
for (let i = 0; i < arr.length; i++) {
process.stdout.write(arr[i] + ', ' );
}
|
Time Complexity: O(log N), where N is the input integer.
Space Complexity: O(digit), where the digit is the number of digits in the input integer.
Approach 3: We can also convert the given number to string and then put each character in the vector.
C++
#include <cstring>
#include <iostream>
using namespace std;
char * convertIntegerToChar( int N)
{
string temp = to_string(N);
int digit = temp.size();
char * arr = new char [digit];
int index = 0;
for ( auto & it : temp) {
arr[index++] = it;
}
arr[index] = '\0' ;
return arr;
}
int main()
{
int N = 12349;
int len = 5;
char * arr = convertIntegerToChar(N);
for ( int i = 0; i < len; i++)
cout << arr[i] << ", " ;
delete [] arr;
return 0;
}
|
Java
import java.util.*;
public class Main
{
public static char [] convertIntegerToChar( int N)
{
String temp = Integer.toString(N);
int len = temp.length();
char [] arr = new char [len];
for ( int i = 0 ; i < len; i++) {
arr[i] = temp.charAt(i);
}
return arr;
}
public static void main(String[] args) {
int N = 12349 ;
int len = 5 ;
char [] arr = convertIntegerToChar(N);
for ( int i = 0 ; i < len; i++) {
System.out.print(arr[i] + ", " );
}
arr = null ;
}
}
|
Python3
def convert_int_to_char(n):
temp = str (n)
length = len (temp)
arr = []
for i in range (length):
arr.append(temp[i])
return arr
a = 12349
res = convert_int_to_char(a)
for i in res:
print (i,end = "," )
|
C#
using System;
class Program
{
static char [] ConvertIntegerToChar( int N)
{
string temp = N.ToString();
int digit = temp.Length;
char [] arr = new char [digit + 1];
for ( int i = 0; i < digit; i++)
{
arr[i] = temp[i];
}
arr[digit] = '\0' ;
return arr;
}
static void Main()
{
int N = 12349;
int len = 5;
char [] arr = ConvertIntegerToChar(N);
for ( int i = 0; i < len; i++)
{
Console.Write(arr[i] + ", " );
}
Console.WriteLine();
Array.Clear(arr, 0, arr.Length);
Console.ReadLine();
}
}
|
Javascript
function convertIntegerToChar(N) {
let temp = N.toString();
let digit = temp.length;
let arr = new Array(digit);
for (let index = 0; index < digit; index++) {
arr[index] = temp.charAt(index);
}
return arr;
}
let N = 12349;
let len = 5;
let arr = convertIntegerToChar(N);
console.log(arr.join( ', ' ));
|
Time Complexity: O(log N), where N is the input integer.
Space Complexity: O(digit), where the digit is the number of digits in the input integer.
Last Updated :
17 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...