Can you write a method in Java which accepts a String argument and returns a number of words in it? A word is a sequence of one or more non-space character i.e. any character other than '' (empty String). This should be your method signature:
public int count(String word);
This method should return 1 if the input is "Java" and return 3 if the input is "Java, C++, Python". Similarly a call to wordCount(" ") should return 0. This is one of the several String algorithmic questions you can expect in a programming job interview. This is used to test the coding skills of the candidate and that's why it's very important to prepare for these questions anytime you go for an interview.
Read more »
public int count(String word);
This method should return 1 if the input is "Java" and return 3 if the input is "Java, C++, Python". Similarly a call to wordCount(" ") should return 0. This is one of the several String algorithmic questions you can expect in a programming job interview. This is used to test the coding skills of the candidate and that's why it's very important to prepare for these questions anytime you go for an interview.