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



Home Answers Viewqa Java-Beginners Program using String functions

Have Programming Question? Ask it here!
 
 
 


soumyadeep
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?

Advertisement
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.in);
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)) {
count++;
}
}
System.out.println(s1 + "-" + count+" times");
}
}

Thanks


Related Tutorials/Questions & Answers:
Advertisements
 

 

 

DMCA.com