Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

Home Answers Viewqa Java-Beginners Program using String functions

Have Programming Question? Ask it here!

Program using String functions
1 Answer(s)      4 years and a month ago
Posted in : Java Beginners

Write a program to accept a sentence and find out how many times the word "the" occurs?

View Answers

August 9, 2010 at 11:05 AM

Hi Friend,

Try the following code:

import java.util.*;

class CountSameWords {

public static void main(String[] args) {
int count = 0;
Scanner input = new Scanner(;
System.out.print("Enter string:");
String st = input.nextLine();
String s1 = "the";
StringTokenizer stk = new StringTokenizer(st);
while (stk.hasMoreTokens()) {
String str = stk.nextToken();
if (s1.equalsIgnoreCase(str)) {
System.out.println(s1 + "-" + count+" times");


Related Tutorials/Questions & Answers: