Write a java program to calculate each word length in sentence :

import java.util.Scanner;
public class CountCharInEachWord {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Sentence ");
String str = sc.nextLine();
charLength(str);
}
static void charLength(String str) {
for(int i=0;i<str.length();i++) {
int k=i;
String temp="";
while(i<str.length() && str.charAt(i)!=' ' ) {
temp=temp+str.charAt(i);
i++;
}
System.out.println(temp+" Length is "+(i-k));
}
}
}

Comments

Popular posts from this blog