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

Run it Now
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("\nCharacter value is :");
        for (int i = 0; i < a.length; i++) {
            System.out.print(" "+a[i]);
        }

    }
}

Output:

Enter a String :smith
Character value is : s m i t h