Convert Array of Integers to Comma-Separated String
Last Updated :
13 Dec, 2023
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;
string arrayToString(vector< int >& arr) {
string answer;
int N = arr.size();
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
import java.util.ArrayList;
import java.util.List;
public class Main {
public static String arrayToString(List<Integer> arr) {
StringBuilder answer = new StringBuilder();
int N = arr.size();
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
def list_to_string(lst):
answer = ""
N = len (lst)
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 {
public static string ListToString(List< int > arr)
{
var answer = new System.Text.StringBuilder();
int N = arr.Count;
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
function arrayToString(arr) {
let answer = "" ;
const N = arr.length;
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;
}
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));
}
main();
|
Output
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)
Share your thoughts in the comments
Please Login to comment...