How to count a number of words in given String in Java?

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.

5 Books to Improve Coding Skills of Programmers - Must Read, Best of Lot

Learning a Programming language e.g. Java or C++ is easy but learning to write good code is not. Writing good code is an art and also an important differentiating factor between an average programmer vs a good programmer. Since most of the programmers often look for inspiration and resource to improve their coding skill, I decide to share some of the good books which can help them to improve their coding. Since many universities, colleges and training courses only teach programming languages but not the art of coding, it still remains one of the self-learned skill. The internet has helped a lot to coders with several websites coming up to teach code, programming contest, helping to solve you programming interview questions and all, but IMHO, books are still vital for overall improvement. In this article, I am going to share some of the great books, written by both great author and great programmers, which can certainly help you to write good code and become a better programmer.

How to Count number of 1s (Set Bits) in Given Bit Sequence in Java

Good morning folks, In today's article, we are going to discuss one of the frequently asked bit manipulation based interview question, how do you count the number of set bits in given bit sequence? It is also asked as how to count the number of 1s in given number? Both are the same question because 1 is also known as set bit.  For example if given input is 1000110010 than your program should return 4, as three are only four set bits in this bit sequence. There are many techniques to solve this problem. Best way to learn why a given algorithm works is to take a pencil and run though a few examples. The solution presented in this article, you might have seen this already in hackers delight, runs through a loop and clears the lowest set bit of number during each iteration. When no set bit is left (i.e. number==0) than the number of iterations is returned. That's your number of 1s or set bits in given bit sequence. Let's learn more about how this algorithm works.