Saturday, January 21, 2012

String to character array in java


The below given program is to convert the string stored in a String variable to a character array.

class StringChar
{
public static void main(String arg[])
{
String str=new String("knowledge");
System.out.println("String= " + str);
char c[]=new char[20];
c=str.toCharArray();
for(int i=0;i<c.length;i++)
System.out.println("c[" + i +"] = " +c[i]);
}
}

The program insert the string("knowledge") stored in variable str to a character array c[20].
Save the program as StringChar.java

No comments :

Post a Comment