Choose Your Language

Wednesday 9 April 2014

java program to display space count of a given string

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

No comments:

Post a Comment