Write a Java program to count the characters in each word in a sentence?
Examples:
Input : geeks for geeks Output : geeks->5 for->3 geeks->5
JAVA:
class CountCharacterInEachWords { static void count(String str) { // Create an char array of given String char[] ch = str.toCharArray(); for (int i = 0; i < ch.length; i++) { // Declare an String with empty initialization String s = ""; // When the character is not space while (i < ch.length && ch[i] != ' ') { // concat with the declared String s = s + ch[i]; i++; } if (s.length() > 0) System.out.println(s + "->" + s.length()); } } public static void main(String[] args) { String str = "geeks for geeks"; count(str); } }
Output:
geeks->5 for->3 geeks->5
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.