Write a java program to count the number of words present in a string?



Write a java program to count the number of words present in a string?..

Answer / Pranay Kumar Suryavanshi

Here is a simple Java program that counts the number of words in a given string. The program splits the string by white spaces and then returns the length of the resulting array.nn```javanpublic class WordCounter {n public static void main(String[] args) {n String str = "This is a sample string";n String[] words = str.split(" ");n System.out.println("Number of words: " + words.length);n }n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

What is the file type?

1 Answers  


is it possible to instantiate the math class?

1 Answers  


In java, how many ways you can take input from the console?

1 Answers  


What is thin driver and thick driver. why it is called so?

2 Answers   Logisoft,


What is a finally block? Is there a case when finally will not execute?

1 Answers  


What is double data type?

1 Answers  


What is the difference between jfc & wfc?

1 Answers  


Name the package that always imported by default?

4 Answers  


What is a pattern what is an anti pattern?

1 Answers  


What does pointer mean?

1 Answers  


What is the use of list in java?

1 Answers  


What is Applet Stub Interface ?

1 Answers  


Categories