nfnmy
nfnmy

Reputation: 157

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

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 (ie other characters) because this is to assume that there would be colons,spaces,tabs, and other signs in the string. So at first my idea was to loop through each character and count how many times that you will not get a letter of an alphabet

    for(int i = 0; i < string.length(); i++) {
      for(int j = 0; i < alphabets.length(); j++) {
       if (string.charAt(i-1) == alphabets.charAt(j)) {
           counter++;
       }
     }
   }

However I will always get an array out of bounds because of this. So, I kinda need a little help or another way that can actually be more efficient. I thought of using Matches to only [a-zA-z] but I'm not sure how do I handle a char to be comparable to a string in counting how many times it occurs.

Thank you

Upvotes: 5

Views: 8280

Answers (8)

Abhishek Kumar
Abhishek Kumar

Reputation: 1015

The following program will count the number of words in a sentence. In this program, we are counting alphabets just after space. The alphabet can be of lower case or upper case. We are inserting a space at the beginning since people don't start a sentence with space. We also need to take care that any special character or number should not be counted as a word.

`import java.util.Scanner; public class WordSent {

public static void main(String[] args) {

    Scanner in= new Scanner(System.in);
    System.out.println("Enter the sentence");
    String str=in.nextLine();
    String space=" ";
    String spaceword=space.concat(str);

    int count=0;
    for(int i=0; i<spaceword.length()-1;i++)
    {
        for (int k=0; k<=25; k++)
        {
        if(spaceword.charAt(i)==' '&& (spaceword.charAt(i+1)==((char)(65+k)) || spaceword.charAt(i+1)==((char)(97+k))))
        {
            count++;
        }
      }
     }
    System.out.println("Total number of words in a sentence are" +" :  "+   count);
     }
    }`

Upvotes: 0

Md. Juyel Rana
Md. Juyel Rana

Reputation: 544

Use just like this

String s = "I am Juyel Rana, from Bangladesh";
int count = s.split(" ").length;

Upvotes: 1

phatfingers
phatfingers

Reputation: 10250

Your suggestion to use a regex like "[A-Za-z]" would work fine. In a split command, you'd split on the inverse, like:

String[] words = "Example test: one, two, three".split("[^A-Za-z]+");

EDIT: If you're just looking for raw speed, this'll do the job more quickly.

public static int countWords(String str) {
    char[] sentence = str.toCharArray();
    boolean inWord = false;
    int wordCt = 0;
    for (char c : sentence) {
        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {
            if (!inWord) {
                wordCt++;
                inWord = true;
            }
        } else {
            inWord = false;
        }
    }
    return wordCt;
}

Upvotes: 2

cheeken
cheeken

Reputation: 34655

This problem is slightly more complicated than your algorithm allows.

  • What if there are two or more spaces in a row?
  • What if the string starts or ends with whitespace (or non-word characters)?

This looks like homework, so I don't want to provide any code. I suggest an alternative approach which is simpler to think about.

  • Walk through the characters in the string, one by one.
  • Do something to remember if you are currently scanning a word or if you are not currently scanning a word.
  • Do something to determine when you enter or leave a word, and increment your counter accordingly.

Upvotes: 2

U2EF1
U2EF1

Reputation: 13259

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.

Upvotes: 1

Gabriel Southern
Gabriel Southern

Reputation: 10063

The reason you are getting an IndexOutOfBoundsException is probably because when i is 0 your inner loop will have string.charAt(i-1) which will throw an exception since 0-1 is -1. If you fix that your method might work, although you can use more efficient techniques.

Upvotes: 1

Miserable Variable
Miserable Variable

Reputation: 28752

   if (string.charAt(i-1) == alphabets.charAt(j)) {
       counter++;
   }

You are incrementing the counter if the character is some alphabet character. You should increment it if it is no alphabet character.

Upvotes: 0

Adam Liss
Adam Liss

Reputation: 48290

You can use String.split() to convert the string into an array, with one word in each element. The number of words is given by the length of the array:

int words = myString.split("\s+").length;

Upvotes: 3

Related Questions