Java word count program

后端 未结 22 1533
北荒
北荒 2020-12-09 06:48

I am trying to make a program on word count which I have partially made and it is giving the correct result but the moment I enter space or more than one space in the string

22条回答
  •  误落风尘
    2020-12-09 07:02

    This could be as simple as using split and count variable.

    public class SplitString {
    
        public static void main(String[] args) {
            int count=0;        
            String s1="Hi i love to code";
    
            for(String s:s1.split(" "))
            {
                count++;
            }
            System.out.println(count);
        }
    }
    

提交回复
热议问题