Q:

Scala program to copy the content of 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 copy characters from string to character array from a specified index. After that, we will print the updated 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 copy the content of 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 copy content of string
// into character array

object Sample {
  def main(args: Array[String]) {
    var str = "ABCDEF";
    var charArr = Array('P', 'Q', 'R', 'S', 'T', 'U');

    var i: Int = 0;

    str.getChars(1, 5, charArr, 1);

    println("Elements of character array: ");
    while (i < 6) {
      printf("%c ", charArr(i));
      i = i + 1;
    }
    println();
  }
}

Output:

Elements of character array: 
P B C D E U 

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 getChars() method to copy the characters from string to character array and then printed the updated 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 the string into a charact... >>
<< Scala program to copy the content of string into a...