Kotlin program to convert character array to string

Kotlin | Character Array to String: Here, we are going to learn how to convert a given character array to a string in Kotlin programming language? Submitted by IncludeHelp, on May 03, 2020

Kotlin - Convert character array to string

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


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

string = "india"

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")


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

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.