Convert Array of Integers to Comma-Separated String
Given an array of integers arr[] of size N, return the string representation of integers separated by comma and a space. The last element should be separated by β and β, instead of comma and a space.
Examples:
Input : arr=[10, 20, 30, 40, 50]
Output: β10, 20, 30, 40 and 50βInput: arr = [2,5]
Output : β2 and 5β³Input: arr = [10]
Output: β10β
Approach: The problem can be solved by using the following approach:
Iterate through the input array and for every integer, convert it to string and append it to the answer string. For every integer, check:
- If the current integer is the last element of the array move out of the loop
- If the current integer is the second last element of array, then append β and β to the answer.
- Else, append a comma and a space to the answer.
After all the iterations return the answer string.
Steps to solve the problem:
- Maintain a string answer to store the final result
- Iterate a loop from 0 to N β 1:
- Convert arr[i] to string and append to answer string
- If i < N β 2, answer = answer + β, β
- If i == N-2, answer = answer + β and β
- Return answer
Below is the implementation of the approach:
C++
#include <iostream> #include <vector> using namespace std; // Method to convert array to string string arrayToString(vector< int >& arr) { // Variable to store the answer string answer; int N = arr.size(); // Iterate over all the elements in arr[] for ( int i = 0; i < N; i++) { answer += to_string(arr[i]); if (i < N - 2) { answer += ", " ; } else if (i == N - 2) { answer += " and " ; } } return answer; } int main() { vector< int > arr = {10, 20, 30, 40, 50}; cout << arrayToString(arr) << endl; arr = {2, 5}; cout << arrayToString(arr) << endl; arr = {10}; cout << arrayToString(arr) << endl; return 0; } |
Java
// Java implementation for the approach import java.util.ArrayList; import java.util.List; public class Main { // Method to convert array to string public static String arrayToString(List<Integer> arr) { // Variable to store the answer StringBuilder answer = new StringBuilder(); int N = arr.size(); // Iterate over all the elements in arr[] for ( int i = 0 ; i < N; i++) { answer.append(arr.get(i)); if (i < N - 2 ) { answer.append( ", " ); } else if (i == N - 2 ) { answer.append( " and " ); } } return answer.toString(); } public static void main(String[] args) { List<Integer> arr = new ArrayList<>(); arr.add( 10 ); arr.add( 20 ); arr.add( 30 ); arr.add( 40 ); arr.add( 50 ); System.out.println(arrayToString(arr)); arr = new ArrayList<>(); arr.add( 2 ); arr.add( 5 ); System.out.println(arrayToString(arr)); arr = new ArrayList<>(); arr.add( 10 ); System.out.println(arrayToString(arr)); } } |
Python3
# Function to convert a list to a formatted string def list_to_string(lst): # Variable to store the answer answer = "" N = len (lst) # Iterate over all the elements in the list for i in range (N): answer + = str (lst[i]) if i < N - 2 : answer + = ", " elif i = = N - 2 : answer + = " and " return answer if __name__ = = "__main__" : arr = [ 10 , 20 , 30 , 40 , 50 ] print (list_to_string(arr)) arr = [ 2 , 5 ] print (list_to_string(arr)) arr = [ 10 ] print (list_to_string(arr)) |
C#
using System; using System.Collections.Generic; public class GFG { // Method to convert list to string public static string ListToString(List< int > arr) { // Variable to store the answer var answer = new System.Text.StringBuilder(); int N = arr.Count; // Iterate over all the elements in arr[] for ( int i = 0; i < N; i++) { answer.Append(arr[i]); if (i < N - 2) { answer.Append( ", " ); } else if (i == N - 2) { answer.Append( " and " ); } } return answer.ToString(); } public static void Main( string [] args) { List< int > arr = new List< int >{ 10, 20, 30, 40, 50 }; Console.WriteLine(ListToString(arr)); arr = new List< int >{ 2, 5 }; Console.WriteLine(ListToString(arr)); arr = new List< int >{ 10 }; Console.WriteLine(ListToString(arr)); } } |
Javascript
// Method to convert array to string function arrayToString(arr) { // Variable to store the answer let answer = "" ; const N = arr.length; // Iterate over all the elements in arr[] for (let i = 0; i < N; i++) { answer += arr[i].toString(); if (i < N - 2) { answer += ", " ; } else if (i === N - 2) { answer += " and " ; } } return answer; } // Main function function main() { let arr = [10, 20, 30, 40, 50]; console.log(arrayToString(arr)); arr = [2, 5]; console.log(arrayToString(arr)); arr = [10]; console.log(arrayToString(arr)); } // Run the main function main(); |
10, 20, 30, 40 and 50 2 and 5 10
Time Complexity: O(N), where N is the size of input array
Auxiliary Space: O(1)