Saturday, April 28, 2018

Java- String toCharArray() method with Example

toCharArray : The method toCharArray(), coverts a string into a sequence of characters or character array. It returns a newly created character array.

Syntax: public char[] toCharArray()

Example: toCharArray() method to reverse a string.


package JavaPkg;

public class String_Reverse {

public static void main(String[] args) {

String str="Java is a simple language";
char[] c=str.toCharArray();
for(int i=c.length-1; i>=0; i--){

 
  System.out.print(c[i]);
}

}

}


Output: egaugnal elpmis a si avaJ




No comments:

Post a Comment