Can yous write a method inwards Java which accepts a String declaration in addition to returns a release of words inwards it? H5N1 give-and-take is a sequence of 1 or to a greater extent than non-space grapheme i.e. whatever grapheme other than '' (empty String). This should move your method signature:
public int count(String word);
This method should provide 1 if the input is "Java" in addition to provide three if the input is "Java, C++, Python". Similarly a telephone band to wordCount(" ") should provide 0. This is 1 of the several String algorithmic questions yous tin appear inwards a programming project interview. This is used to exam the coding skills of the candidate in addition to that's why it's real of import to gear upwards for these questions anytime yous cash inwards one's chips for an interview.
Many programmers conduct hold personal collections of such questions which they revise every fourth dimension they cash inwards one's chips for a Programming project interview, but if yous don't conduct hold whatever yet, in addition to thence don't worry. You tin ever operate the Cracking the Coding Interview, it contains 190 programming questions in addition to solutions on every of import topic e.g. String, array, linked list, hash table, binary tree in addition to other information structures.
Though, yous demand to move a fiddling flake careful because at that topographic point is besides a possibility of to a greater extent than than 1 infinite betwixt 2 words. So exactly using a regular facial expression to select grip of infinite or tab volition non move enough, yous demand to operate a greedy regular expression to select grip of multiple spaces every bit well.
In Java, yous tin operate the regular facial expression blueprint "\\s+" to select grip of multiple spaces. The "\s" is a grapheme bird to uncovering white space, which could represent both infinite in addition to tabs in addition to "+" makes it greedy because it volition represent 1 or to a greater extent than of "\s" blueprint i.e. 1 or to a greater extent than space. Now, since yous demand to escape the "\" backward slash inwards Java, the whole blueprint becomes "\\s+". Se Mastering regular expression to larn to a greater extent than near regex inwards Java.
Java Program to count release of words inwards String
That's all near how to count a release of words inwards a given String inwards Java. The regular facial expression fob actually solves this work inwards a distich of lines, but every bit a challenge, tin yous solve this work without using regular expression? If yous maintain existent interview, that would move sure enough a follo-up questions given how slowly the split() method solves this problem. So, why non elbow grease it now? You tin besides banking concern check the solution here, in 1 trial yous conduct hold tried in addition to solved it yourself.
public int count(String word);
This method should provide 1 if the input is "Java" in addition to provide three if the input is "Java, C++, Python". Similarly a telephone band to wordCount(" ") should provide 0. This is 1 of the several String algorithmic questions yous tin appear inwards a programming project interview. This is used to exam the coding skills of the candidate in addition to that's why it's real of import to gear upwards for these questions anytime yous cash inwards one's chips for an interview.
Many programmers conduct hold personal collections of such questions which they revise every fourth dimension they cash inwards one's chips for a Programming project interview, but if yous don't conduct hold whatever yet, in addition to thence don't worry. You tin ever operate the Cracking the Coding Interview, it contains 190 programming questions in addition to solutions on every of import topic e.g. String, array, linked list, hash table, binary tree in addition to other information structures.
Solution - Counting words inwards String
The solution of this work is real uncomplicated if yous know a fiddling flake of regular facial expression in addition to how to split upwards String inwards Java using regex (see here). Since work says that words are separated past times white space, which could move infinite or tabs. So if yous split upwards the String past times whitespace every bit shown here, yous tin acquire an array of String which is null but words. Now the length of this array is your release of words, exactly provide that.Though, yous demand to move a fiddling flake careful because at that topographic point is besides a possibility of to a greater extent than than 1 infinite betwixt 2 words. So exactly using a regular facial expression to select grip of infinite or tab volition non move enough, yous demand to operate a greedy regular expression to select grip of multiple spaces every bit well.
In Java, yous tin operate the regular facial expression blueprint "\\s+" to select grip of multiple spaces. The "\s" is a grapheme bird to uncovering white space, which could represent both infinite in addition to tabs in addition to "+" makes it greedy because it volition represent 1 or to a greater extent than of "\s" blueprint i.e. 1 or to a greater extent than space. Now, since yous demand to escape the "\" backward slash inwards Java, the whole blueprint becomes "\\s+". Se Mastering regular expression to larn to a greater extent than near regex inwards Java.
Java Program to count release of words inwards String
/** * Java Program to count release of words inwards a given * sentence. Words are separated past times whitespace inwards * String. * @author WINDOWS 8 * */ public class StringCounter { public static void main(String[] args) { // testing amongst non-empty String String input = "Java is great"; int numOfWords = count(input); System.out.println("input: " + input); System.out.println("count of words: " + numOfWords); // testing amongst empty String input = ""; numOfWords = count(input); System.out.println("input: " + input); System.out.println("count of words: " + numOfWords); // testing amongst null String input = null; numOfWords = count(input); System.out.println("input: " + input); System.out.println("count of words: " + numOfWords); } /** * Return the release of words inwards given String * @param sentence, words are separated past times whitespace * @return count of words inwards judgement */ public static int count(String sentence){ if(sentence == null || sentence.isEmpty()){ return 0; } String[] words = sentence.split("\\s+"); return words.length; } } Output: input: Java is slap-up count of words: three input: count of words: 0 input: null count of words: 0
That's all near how to count a release of words inwards a given String inwards Java. The regular facial expression fob actually solves this work inwards a distich of lines, but every bit a challenge, tin yous solve this work without using regular expression? If yous maintain existent interview, that would move sure enough a follo-up questions given how slowly the split() method solves this problem. So, why non elbow grease it now? You tin besides banking concern check the solution here, in 1 trial yous conduct hold tried in addition to solved it yourself.
Other String coding Interview questiosns from Programming Interviews:
- How to contrary String inwards Java without StirngBuffer? (solution)
- How to count vowels in addition to consonants inwards given String? (solution)
- How to uncovering duplicate characters on String? (solution)
- How to banking concern check if a Stirng is palindrome inwards Java? (solution)
- How to contrary words inwards a given String inwards Java? (solution)
- How to impress all permutations of a String using recursion? (solution)

0 Response to "How to count a lay out of words inwards given String inwards Java?"
Posting Komentar