by Dinesh Thakur Category: array

toCharArray():-This method is a predefined method of String present in lang package.It used to convert a String into array of character.

Syntax:-public char[] toCharArray()

Algorithm for toCharArray() in java:


step 1: read string s1

step 2: create a character array c[]=sl.toCharArray()

step 3: initialize i=0

step 4: repeat through step-6 while (i < s1.length)

step 5: print (byte)c[i]

step 6: i=i+ 1

step 7: Exit

Here is the Java Example for toCharArray() in java:

import java.util.Scanner;

public class toCharArray
              public static void main(String args[])
                           Scanner s=new Scanner(;
                           System.out.print("Enter the String : ");
                           String s1=s.nextLine();
                           char c[]=s1.toCharArray();
                                 for(int i=0;i<s1.length();i++)
                                              System.out.println(c[i]+" = "+(byte)c[i]);

toCharArray() in java

Popular Article