write a menu driven program to find the frequency of "and","the","to" or any input word in a string of text
import java.util.*; class StringExample1 { public static void main(String[] args) { int count1=0,count2=0,count3=0; Scanner input=new Scanner(System.in); System.out.print("Enter string: "); String str=input.nextLine(); String s[]=str.split(" "); System.out.println("Choose------------"); System.out.println("1 and"); System.out.println("2 the"); System.out.println("3 two"); System.out.print("Enter your choice: "); int choice=input.nextInt(); switch(choice){ case 1: for(int i=0;i<s.length;i++){ if(s[i].equals("and")){ count1++; } } System.out.println("No of and: "+count1); break; case 2: for(int i=0;i<s.length;i++){ if(s[i].equals("the")){ count2++; } } System.out.println("No of the: "+count2); break; case 3: for(int i=0;i<s.length;i++){ if(s[i].equals("to")){ count3++; } } System.out.println("No of to: "+count3); break; } } }
Ads