Convert given Array of Integers into Words
Last Updated :
08 Mar, 2022
Given an array arr[] of N elements which are in range [0, 9]. the task is to convert each array element into its numeric strings.
Examples:
Input: arr[] = [1, 4, 3, 2, 6]
Output: one four three two six
Input: arr[]= [0, 4, 4, 6, 9]
Output: zero four four six nine
Approach: The problem can be solved with the help of map. Follow the steps given below:
- Map each number from 1 to 9 to their respective numeric strings.
- Iterate through the array and change each number to the string to which it is mapped.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void convert( int arr[], int N)
{
map< int , string> mp;
int i;
mp[0] = "zero" ;
mp[1] = "one" ;
mp[2] = "two" ;
mp[3] = "three" ;
mp[4] = "four" ;
mp[5] = "five" ;
mp[6] = "six" ;
mp[7] = "seven" ;
mp[8] = "eight" ;
mp[9] = "nine" ;
for (i = 0; i < N; i++)
cout << mp[arr[i]] << " " ;
cout << endl;
}
int main()
{
int arr[] = { 1, 4, 3, 2, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
convert(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void convert( int arr[], int N)
{
HashMap<Integer, String> mp = new HashMap<>();
int i;
mp.put( 0 , "zero" );
mp.put( 1 , "one" );
mp.put( 2 , "two" );
mp.put( 3 , "three" );
mp.put( 4 , "four" );
mp.put( 5 , "five" );
mp.put( 6 , "six" );
mp.put( 7 , "seven" );
mp.put( 8 , "eight" );
mp.put( 9 , "nine" );
for (i = 0 ; i < N; i++)
System.out.print(mp.get(arr[i]) + " " );
System.out.println();
}
public static void main(String[] args)
{
int arr[] = new int [] { 1 , 4 , 3 , 2 , 6 };
int N = arr.length;
convert(arr, N);
}
}
|
Python3
def convert(arr, N):
mp = {};
mp[ 0 ] = "zero" ;
mp[ 1 ] = "one" ;
mp[ 2 ] = "two" ;
mp[ 3 ] = "three" ;
mp[ 4 ] = "four" ;
mp[ 5 ] = "five" ;
mp[ 6 ] = "six" ;
mp[ 7 ] = "seven" ;
mp[ 8 ] = "eight" ;
mp[ 9 ] = "nine" ;
for i in range (N):
print (mp[arr[i]], end = " " );
arr = [ 1 , 4 , 3 , 2 , 6 ];
N = len (arr);
convert(arr, N);
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void convert( int []arr, int N)
{
Dictionary< int , string > mp =
new Dictionary< int , string >();
int i;
mp[0] = "zero" ;
mp[1] = "one" ;
mp[2] = "two" ;
mp[3] = "three" ;
mp[4] = "four" ;
mp[5] = "five" ;
mp[6] = "six" ;
mp[7] = "seven" ;
mp[8] = "eight" ;
mp[9] = "nine" ;
for (i = 0; i < N; i++)
Console.Write(mp[arr[i]] + " " );
Console.WriteLine();
}
public static void Main()
{
int []arr = { 1, 4, 3, 2, 6 };
int N = arr.Length;
convert(arr, N);
}
}
|
Javascript
<script>
const convert = (arr, N) => {
let mp = {};
let i;
mp[0] = "zero" ;
mp[1] = "one" ;
mp[2] = "two" ;
mp[3] = "three" ;
mp[4] = "four" ;
mp[5] = "five" ;
mp[6] = "six" ;
mp[7] = "seven" ;
mp[8] = "eight" ;
mp[9] = "nine" ;
for (i = 0; i < N; i++)
document.write(`${mp[arr[i]]} `);
document.write( "<br/>" );
}
let arr = [1, 4, 3, 2, 6];
let N = arr.length;
convert(arr, N);
</script>
|
Output
one four three two six
Time Complexity: O(N).
Auxiliary Space: O(N).
Share your thoughts in the comments
Please Login to comment...