tag:blogger.com,1999:blog-8712770457197348465.post9209245296690832195..comments2024-03-28T18:10:34.431-07:00Comments on Javarevisited: How to Count Number of Words in String ? Java Coding Exercise Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger18125tag:blogger.com,1999:blog-8712770457197348465.post-69009062284603269342019-06-04T01:18:12.172-07:002019-06-04T01:18:12.172-07:00Java code for first non-repeating character in a g...Java code for first non-repeating character in a given string by using toCharArray()Sonuhttps://www.blogger.com/profile/05711139202880004904noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-85256394532866231802017-07-03T11:25:04.024-07:002017-07-03T11:25:04.024-07:00class WordCount
{
int wordcount(String s)
{
i...class WordCount<br />{<br /> int wordcount(String s)<br /> {<br /> int x;<br /> String[] s1;<br /> s1=s.split(" ");<br /> int count=0;<br /> <br /> for(int i=0;i<s1.length;i++)<br /> {<br /> if(!(s1[i].isEmpty()))<br /> {<br /> <br /> <br /> count++;<br /> }<br /> }<br /> <br /> return count;<br /> }<br /> public Anonymoushttps://www.blogger.com/profile/14305856283270782480noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-14576078397385282162016-11-17T23:06:58.698-08:002016-11-17T23:06:58.698-08:00public class WordCount2 {
public int wordcouno...public class WordCount2 {<br /> public int wordcounot (String word){<br /> if (word==null || word.isEmpty()){<br /> return 0;<br /> }<br /> int count =0;<br /> char ch[] = new char[word.length()];<br /> for(int i = 0; i< word.length();i++){<br /> <br /> }<br /> }<br /><br /> }<br />Pramod Nyachyonhttps://www.blogger.com/profile/06072629689213344590noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45622265238353505292016-11-04T14:15:24.151-07:002016-11-04T14:15:24.151-07:00Javin Sir,
What if i write "JAVINISBEST"...Javin Sir, <br />What if i write "JAVINISBEST" without spaces<br />and i want to get number of words as 3?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65297080441522178972016-07-20T05:22:48.131-07:002016-07-20T05:22:48.131-07:00split and length!!! all you needsplit and length!!! all you needAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-45168319864353954682016-06-06T21:15:34.779-07:002016-06-06T21:15:34.779-07:00@Niya, what are bugs? I don't see any problem ...@Niya, what are bugs? I don't see any problem with the code? do you know any particular scenario?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-60509650249257191342016-02-17T11:08:14.260-08:002016-02-17T11:08:14.260-08:00BuggyBuggyNiyahttps://www.blogger.com/profile/00040553131109417192noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-27916838865278033442015-07-24T17:25:40.821-07:002015-07-24T17:25:40.821-07:00@Anonymous (May 24), very good solution. good nami...@Anonymous (May 24), very good solution. good naming convention, I like using whitespace variable, makes it more readable. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-73514818365361877142015-07-24T17:24:13.489-07:002015-07-24T17:24:13.489-07:00@Kunal, and @Annonymous (1st two) that was bug int...@Kunal, and @Annonymous (1st two) that was bug introduced during formatting. It should be (i > 0) && (ch[i] != ' '). That's why you can see that even though JUnit test passed, first method always return 1. Correct it now. <br />javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72648905622622254342015-07-24T17:16:44.445-07:002015-07-24T17:16:44.445-07:00@k.k, your program will count empty string as word...@k.k, your program will count empty string as word. It is also not considering words separated by multiple spaces. javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-24679241421433407082015-07-24T17:15:22.474-07:002015-07-24T17:15:22.474-07:00@Anonymous, regular expression is indeed best way ...@Anonymous, regular expression is indeed best way to count number of words in String. By the way, you don't need to call s=s.replaceAll("\\s+"," ");, you can directly split, words separated by multiple space will also be found by greedy match i..e \\s+javin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-29509790590146704742015-07-24T02:36:56.839-07:002015-07-24T02:36:56.839-07:00public class WordCount {
public static void ma...public class WordCount {<br /><br /> <br /> public static void main(String[] args) {<br /> <br /><br /> String s="are you ready ";<br /> findCount(s); <br /> <br /> }<br /><br /> private static void findCount(String s) {<br /> // TODO Auto-generated method stub<br /> <br /> s=s.replaceAll("\\s+"," ");<br /> String []s1=s.split(" ");<br /> Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32547845072758424152015-06-07T03:27:57.754-07:002015-06-07T03:27:57.754-07:00import java.io.*;
class CountWords
{
public s...import java.io.*;<br />class CountWords <br />{<br /> public static void main (String[] args)<br /> {<br /><br /> System.out.println("total number of words in string");<br /><br /> String s = "happy bday bhai";<br /><br /> int wordCount = 1;<br /><br /> for (int i = 0; i < s.length(); i++) <br /> {<br />Anonymoushttps://www.blogger.com/profile/15490686220569945017noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-90604006607688414422015-05-24T22:27:01.905-07:002015-05-24T22:27:01.905-07:00public static int countWords(String s){
if(s==nu...public static int countWords(String s){<br /> if(s==null || s.isEmpty()){<br /> return 0;<br /> }<br /> int count=0;<br /> boolean iswhitespace=true;<br /> char[] arr =s.toCharArray();<br /> for(int i=0; i<arr.length; i++){<br /> if((iswhitespace==true) && arr[i]!=' '){<br /> count++;<br /> iswhitespace= false; <br /> }else if(arr[i]==' '){<br />Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-83150691045618861762015-04-02T01:18:33.451-07:002015-04-02T01:18:33.451-07:00best fit:
String input= "Java is best prog...best fit:<br /> String input= "Java is best programming language";<br /> System.out.println(input.split( "\\s" ).length);Anonymoushttps://www.blogger.com/profile/15021063606768028203noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-75736084207556287912015-02-27T20:25:42.443-08:002015-02-27T20:25:42.443-08:00Hi, but first method returns 1 always!Hi, but first method returns 1 always!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-21858679606232561802015-02-27T07:50:26.358-08:002015-02-27T07:50:26.358-08:00Should be
if (((i > 0) ...
Right?Should be<br /><br />if (((i > 0) ...<br /><br />Right?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-1690003846527476502015-02-27T06:27:22.150-08:002015-02-27T06:27:22.150-08:00Why do (i < 0) && (ch[i] != ' '...Why do (i < 0) && (ch[i] != ' ')<br /><br />when i is assured it will never be negative. i starts from 0 and is never decremented. that's a bogus check to have.Kunal Krishna https://www.blogger.com/profile/06202237214255825462noreply@blogger.com