Let’s see a program to print the first character of each word.
package program; public class PrintFirstCharecter { public static void main(String[] args) { String str = "my name is anjali"; char[] c = str.toCharArray(); for (int i = 0; i < c.length; i++) { if (c[i] != ' ' && (i == 0 || c[i - 1] == ' ')) { System.out.println(c[i]); } } } }
Output is:-
m
n
i
a