Count words in a string method?

Count words in a string method?

How do you Count words in a string?

View Answers

June 28, 2012 at 5:59 PM

Example:

package RoseIndia;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class StringCount {
    public static void main(String [] args){
  BufferedReader br = 
          new BufferedReader(new InputStreamReader(System.in));
     System.out.print("Enter string: ");
  try{
     String st=br.readLine();

     int count=0;

     String arr[]=st.split(" ");

     System.out.println("Number of words : "+arr.length);
      }catch(IOException e){
    e.printStackTrace();
     }
    }
}

Output:

Enter string: hi how are you
Number of words : 4

Description: This example counts the number of words into inputted String. Here st is user-inputted string. BufferedReader is used for user input. split()method is used for splitting the given string according to given pattern. In above example we are splitting st by pattern ?? ?? ie by space. arr[] is String type array in which we are storing string after splitting. Now count the length of arr which return the number of words in the inputted string.









Related Tutorials/Questions & Answers:
Count words in a string method?
how to count words in string using java
Advertisements
Count numbers of spaces and number of words from the input string
Java count frequency of words in the string
ModuleNotFoundError: No module named 'the-count-of-words'
Breaking the String into Words
Searching English words in a string
JavaScript split string into words.
JavaScript Count Words
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .
reverse words in a string(words seperated byone or more spaces)
String Methods - Java Beginners
Find number of words begin with the specified character
A Program To Reverse Words In String in Java
J2ME count character into string
java program to insert data into a file and count the number of words from the file???????
Program to count the number of unique words in a file using HashMap
Java count words from file
count the repeated character in one string
Retrieve a list of words from a website and show a word count plus a specified number of most frequently occurring words
Breaking a string into words without using StringTokenizer
Java Reverse words of the String
Java reverse words in a string using only loops
String toLowerCase and toUppercase methods example
Count Palindromes from the string
Count Characters from the input string Java
Writing code with multiple Class Methods and String Arrays
Writing code with multiple Class Methods and String Arrays
Java count vowels
string
String copyValueOf(char[] data, int offset, int count)
Java Word Count - Word Count Example in Java
methods
methods
methods
Count letters in a string.
Java Count Vowels
read string - using loops in Java
Java : Length of String
String Functions In Java
String replaceAll() Method
string
Count number of "*"
count characters
string
string - Java Beginners

Ads