Q:

Kotlin program to convert character array to string

belongs to collection: Kotlin Array Programs

0

Given a character array, we have to convert it into a string.

Example:

    Input:
    char array: ['i', 'n', 'd', 'i', 'a']

    Output:
    string = "india"

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to convert character array to string in Kotlin

package com.includehelp.basic

import java.util.*

//Main Function entry Point of Program
fun main(args: Array<String>) {
    //Input Stream
    val scanner = Scanner(System.`in`)

    //Input Char Array Size
    print("Enter Char Array size: ")
    val size = scanner.nextInt()

    //Create Character array of Given size
    val charArray = CharArray(size)

    //Input array elements
    println("Enter Char Arrays Elements:")
    for (i in charArray.indices) {
        print("charArray[$i] : ")
        charArray[i] = scanner.next()[0]
    }

    //Print All Array Elements
    println("Array : ${charArray.contentToString()} ")

    //Get String from Cahr Array
    var strFromArray = String(charArray)

    //print String from Array
    println("String Converted from Array  : $strFromArray")
}

Output

Run 1:
-----
Enter Char Array size: 11
Enter Char Arrays Elements:
charArray[0] : i
charArray[1] : n
charArray[2] : c
charArray[3] : l
charArray[4] : u
charArray[5] : d
charArray[6] : e
charArray[7] : h
charArray[8] : e
charArray[9] : l
charArray[10] : p
Array : [i, n, c, l, u, d, e, h, e, l,p]
String Converted from Array  : includehelp
-----
Run 2:
----
Enter Char Array size: 5
Enter Char Arrays Elements:
charArray[0] : i
charArray[1] : n
charArray[2] : d
charArray[3] : i
charArray[4] : a
Array : [i, n, d, i, a]
String Converted from Array  : india

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Kotlin program to convert string to character arra... >>