Java word count program

后端 未结 22 1557
北荒
北荒 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:12

    public class wordCOunt
    {
    public static void main(String ar[])
    {
    System.out.println("Simple Java Word Count Program");
    
        String str1 = "Today is Holdiay Day";
    
        int wordCount = 1;
    
        for (int i = 0; i < str1.length(); i++) 
        {
            if (str1.charAt(i) == ' '&& str1.charAt(i+1)!=' ') 
            {
                wordCount++;
            } 
        }
    
        System.out.println("Word count is = " +(str1.length()- wordCount));
    }
    

    }

提交回复
热议问题