Java > AP-1 > wordsCount (CodingBat Solution)

Problem:

Given an array of strings, return the count of the number of strings with the given length.

wordsCount({"a", "bb", "b", "ccc"}, 1) → 2
wordsCount({"a", "bb", "b", "ccc"}, 3) → 1
wordsCount({"a", "bb", "b", "ccc"}, 4) → 0


Solution:

public int wordsCount(String[] words, int len) {
  int count = 0;  
  for (int i = 0; i < words.length; i++) {
    if (words[i].length() == len)
      count++;
  }
  return count;
}


4 comments :

  1. int count=0;
    for(String w : words){
    if(w.length()==len) count++;
    }
    return count;

    ReplyDelete
  2. public int wordsCount(String[] words, int len) {
    int count = 0;
    for (final String word : words) {
    if (word.length() == len) {
    count++;
    }
    }
    return count;
    }

    ReplyDelete
  3. public int wordsCount(String[] words, int len) {
    int c=0;
    for(int i=0;i<words.length;i++)
    if(words[i].length()==len)
    c++;
    return c;
    }

    ReplyDelete
  4. public int wordsCount(String[] words, int len)
    {
    int counter = 0;

    for(int i = 0; i < words.length; i++)
    {
    if(words[i].length() == len) counter++;
    }

    return counter;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact