(Coderbyte) Word Count - Solução

Have the function WordCount(str) take the str string parameter being passed and return the number of words the string contains (ie. "Never eat shredded wheat" would return 4). Words will be separated by single spaces.

Solução:

import java.util.*;
import java.io.*;

class Function { 
  int WordCount(String str) {
    String[] word = str.split(" ");
       
    return word.length;
  }
 
  public static void main (String[] args) { 
    // keep this function call here    
    Scanner  s = new Scanner(System.in);
    Function c = new Function();
    System.out.print(c.WordCount(s.nextLine()));
  }  
 
}          

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Powers of Two - Solução

(CoderByte) Number Search - Solução