//Program to calculate how many times a word occurs in a given string in just one line
import java.io.*; //for IOException, BufferedReader, InputStreamReader
class CountWord
{
public static void main(String args[]) throws IOException
{
System.out.println("Enter the string: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine(); //Gets a string from user
System.out.println("Enter the word to search for: ");
String word=br.readLine(); //Gets a string from user
int count=(s.length()-s.replace(word+"", "").length())/word.length();
System.out.println(word+" occurs in given string for: "+count+" times");
}
}
By Gurpreet Singh, Department of Computer Science & Engineering, Amritsar College of Engineering & Technology
Sunday, 14 June 2015
Program to calculate how many times a word occurs in a given string in just one line
Labels:
Java,
JavaPrograms
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment