Scala program to copy the content of string into a character array

Here, we are going to learn how to copy the content of string into a character array in Scala programming language?
Submitted by Nidhi, on May 22, 2021 [Last updated : March 10, 2023]

Scala – Copy the String to Character Array

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.

Scala code to copy the content of string into a character array

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 

Explanation

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.

Scala String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





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