Java Program to convert string to char array


In this program, You will learn how to convert string to char array in java.


char a[] = str.toCharArray();

Example: How to convert string to char array in java.

import java.util.Scanner;

class Main {
    public static void main(String args[]) {

        Scanner s = new Scanner(System.in);
        String str;

        System.out.print("Enter a string:");
        str = s.nextLine();

        char a[] = str.toCharArray();

        System.out.print("Character value are:");
        for (int i = 0; i < a.length; i++) {
            System.out.print(" "+a[i]);
        }

    }
}

Output:

Enter a string:Xiith.com
Character value are: X i i t h . c o m