How do I count the number of words in a string?

后端 未结 8 1640
感情败类
感情败类 2021-01-13 02:54

I need to count the number of words and I am assuming the correct way to do it is by calculating the number of times that the previous character in a string is not a letter

8条回答
  •  轮回少年
    2021-01-13 03:26

    Addressing the code directly, your first loop has i=0 as the first value of i, but then you ask for

    string.charAt(i-1) = string.charAt(-1),

    which is where your array-out-of-bounds is coming from.

    The second loop has another problem:

    for(int j = 0; i < alphabets.length(); j++) {

    You may also want to consider apostrophes as parts of words as well.

提交回复
热议问题