Kotlin program to convert string to character array

Kotlin | Convert string to character array: Here, we are going to learn how to convert a given string to the character array in Kotlin programming language? Submitted by IncludeHelp, on May 05, 2020

Kotlin - Convert string to character array

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

Example:

Input:
string = "IncludeHelp India Pvt Ltd"

Output:
char_arr = [I, n, c, l, u, d, e, H, e, l, p,  , I, n, d, i, a,  , P, v, t,  , L, t, d]

Program to convert string to character array in Kotlin

package com.includehelp

import java.util.*

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

    //Input String
    print("Enter String : ")
    var str = scanner.nextLine()

    //convert String to Character Array
    val charArray = str.toCharArray()

    //Print Character Array get from String
    println("Character Array Elements  : ${charArray.contentToString()} ")
}

Output

Run 1:
-----
Enter String : IncludeHelp India Pvt Ltd
Character Array Elements  : [I, n, c, l, u, d, e, H, e, l, p,  , I, n, d, i, a,  , P, v, t,  , L, t, d]
--------
Run 2:
----
Enter String : Corona Virus Covid-19 Panidimic
Character Array Elements  : [C, o, r, o, n, a,  , V, i, r, u, s,  , C, o, v, i, d, -, 1, 9,  , P, a, n, i, d, i, m, i, c]

Comments and Discussions!

Load comments ↻





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