Open In App

Convert given Array of Integers into Words

Last Updated : 08 Mar, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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++
// C++ code to implement the approach

#include <bits/stdc++.h>
using namespace std;

// Function to convert
// the numeric array to words
void convert(int arr[], int N)
{
    map<int, string> mp;
    int i;

    // Map the integers to
    // their respective numeric  string
    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";

    // Traverse array elements and print
    for (i = 0; i < N; i++)
        cout << mp[arr[i]] << " ";

    cout << endl;
}

// Driver Code
int main()
{
    int arr[] = { 1, 4, 3, 2, 6 };
    int N = sizeof(arr) / sizeof(arr[0]);

    // Function call
    convert(arr, N);
    return 0;
}
Java
// JAVA code to implement the approach
import java.util.*;
class GFG {

  // Function to convert
  // the numeric array to words
  public static void convert(int arr[], int N)
  {
    HashMap<Integer, String> mp = new HashMap<>();
    int i;

    // Map the integers to
    // their respective numeric  string
    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");

    // Traverse array elements and print
    for (i = 0; i < N; i++)
      System.out.print(mp.get(arr[i]) + " ");

    System.out.println();
  }

  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = new int[] { 1, 4, 3, 2, 6 };
    int N = arr.length;

    // Function call
    convert(arr, N);
  }
}

// This code is contributed by Taranpreet
Python3
# Python code for the above approach
# Function to convert
# the numeric array to words
def convert(arr, N):
    mp = {};
    
    # Map the integers to
    # their respective numeric string
    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";

     # Traverse array elements and print
    for i in range(N):
        print(mp[arr[i]], end = " ");

# Driver Code
arr = [1, 4, 3, 2, 6];
N = len(arr);

# Function call
convert(arr, N);

# This code is contributed by Potta Lokesh
C#
// C# code to implement the approach
using System;
using System.Collections.Generic;

class GFG
{

  // Function to convert
  // the numeric array to words
  static void convert(int []arr, int N)
  {
    Dictionary<int, string> mp =  
      new Dictionary<int, string>();
    int i;

    // Map the integers to
    // their respective numeric  string
    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";

    // Traverse array elements and print
    for (i = 0; i < N; i++)
      Console.Write(mp[arr[i]] + " ");

    Console.WriteLine();
  }

  // Driver Code
  public static void Main()
  {
    int []arr = { 1, 4, 3, 2, 6 };
    int N = arr.Length;

    // Function call
    convert(arr, N);
  }
}

// This code is contributed by Samim Hossain Mondal.
JavaScript
    <script>
        // JavaScript code to implement the approach


        // Function to convert
        // the numeric array to words
        const convert = (arr, N) => {
            let mp = {};
            let i;

            // Map the integers to
            // their respective numeric string
            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";

            // Traverse array elements and print
            for (i = 0; i < N; i++)
                document.write(`${mp[arr[i]]} `);

            document.write("<br/>");
        }

        // Driver Code

        let arr = [1, 4, 3, 2, 6];
        let N = arr.length;

        // Function call
        convert(arr, N);

    // This code is contributed by rakeshsahni

    </script>

Output
one four three two six 

Time Complexity: O(N).
Auxiliary Space: O(N).


Next Article
Practice Tags :

Similar Reads