Write a program to find the score of repeating words/ word repetition


Program
import java.util.*; public class PrintMessage { public static void main (String[] args) { Scanner sc=new Scanner(System.in); System.out.println("Enter the string"); String s=sc.next(); int count=0; int sum=2; for(int i=0;i<s.length();i++) { if(Character.isDigit(s.charAt(i)) || Character.isLetter(s.charAt(i)) || s.charAt(i)==' ') { continue; } else { System.out.println(s+" is not valid String"); return; } } System.out.println("Enter the sentence"); sc.nextLine(); String sentence=sc.nextLine(); String[] w=sentence.split(" "); for(int i=0;i<w.length;i++) { if(w[i].equalsIgnoreCase(s)) { count++; if(count>2) { sum=sum*2; } } } if(count>2) { System.out.println("Score is "+sum); } else { System.out.println("Score is "+count); } } }
Input
Output

Similar Program