Q:

Scala program to convert the string into a character array

belongs to collection: Scala String Programs

0

Here, we will create a string and a character array and then we will convert the string into a character array using the toCharArray() method and then print the character array on the console screen.

All Answers

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

Program/Source Code:

The source code to convert the string into a character array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to convert string into
// character array

object Sample {
  def main(args: Array[String]) {
    var str = "ABCDEF";
    var charArr = Array[Char](6)

    var i: Int = 0;

    charArr = str.toCharArray();

    println("Character array: ");
    while (i < charArr.length) {
      printf("%c ", charArr(i));
      i = i + 1;
    }
    println();
  }
}

Output:

Character array: 
A B C D E F

In the above program, we used an object-oriented approach to create the program. And, we created an object Sample. Here, we defined main() function. The main() function is the entry point for the program.

In the main() function, we created a string str and a character array charArr. And, we used the toCharArray() method to convert the string into a character array. After that, we printed the character array on the console screen.

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

total answers (1)

Scala String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Scala program to convert an integer number into a ... >>
<< Scala program to copy the content of string into a...