Java program to convert character to string and vice versa
Here's an example Java program to convert a character to a string and vice versa:
rfeer to:theitroad.compublic class CharacterStringConversion { public static void main(String[] args) { // Convert character to string char c = 'a'; String s = Character.toString(c); System.out.println(s); // Output: a // Convert string to character String str = "hello"; char[] chars = str.toCharArray(); for (char ch : chars) { System.out.println(ch); } } }
This program demonstrates two methods for converting between characters and strings. To convert a character to a string, the Character.toString() method is used. To convert a string to a character, the String.toCharArray() method is used to convert the string to an array of characters, which can then be looped over to access each individual character.
In the example above, the character 'a' is converted to the string "a" using the Character.toString() method, and then printed to the console. The string "hello" is converted to an array of characters using the toCharArray() method, and then each character in the array is printed to the console.