public class SpaceCounter{
static int spaceCounter=1;
public static void main(String ar[]){
String input="hello aravind sankaran nair";
for (char space : input.toCharArray()) {
if(Character.isWhitespace(space)){
spaceCounter=spaceCounter+1;
}
}
System.out.println("number of words = "+spaceCounter);
}
}
output:
number of words = 4
static int spaceCounter=1;
public static void main(String ar[]){
String input="hello aravind sankaran nair";
for (char space : input.toCharArray()) {
if(Character.isWhitespace(space)){
spaceCounter=spaceCounter+1;
}
}
System.out.println("number of words = "+spaceCounter);
}
}
output:
number of words = 4
No comments:
Post a Comment