Let’s see program to count characters in each word of given String.
package programnew; public class CharacterCount { public static void count(String s) { char[] c = s.toCharArray(); for (int i = 0; i < c.length; i++) { String str = ""; while (i < c.length && c[i] != ' ') { str = str + c[i]; i++; } if (str.length() > 0) { System.out.println("Charactars in " + str + "--- " + str.length()); } } } public static void main(String[] args) { String s = "my name is anjali"; count(s); } }
Output is :-
Characters in my— 2
Characters in name— 4
Characters in is— 2
Characters in anjali— 6