The document contains a Java program that prints the frequency of duplicate words in a given string. It uses a Scanner to read input, processes the string to identify and count repeated words, and outputs the frequency of each duplicate. The program utilizes string manipulation methods to achieve its functionality.
The document contains a Java program that prints the frequency of duplicate words in a given string. It uses a Scanner to read input, processes the string to identify and count repeated words, and outputs the frequency of each duplicate. The program utilizes string manipulation methods to achieve its functionality.
public static void main(String[] args) //declaring main method
{ //opening braces of main method
Scanner pa = new Scanner(System.in); //declaring scanner class
System.out.println("Enter a string:");
String s = pa.nextLine().toLowerCase()+" "; //Storing String into LowerCase
int e; //Variable store the index of space
for(int i=0;i<s.length();i+=e) //Loop till string length
e = s.indexOf(' ',i); //To store index of Space
String w = s.substring(i,e); //To store string between i and e
int d = s.indexOf(w, e + 1); //to check if word is repeated in the string
if (d>0) //if String is repeated
System.out.println(w+"-"+d); //print the frequncy of word
} //closing braces of main method
} //closing braces of class
VARIABLE DATA TYPE FUNCTION
s String To store the String e Character To store the index of space i Integer To store the index of the first character of the word w Integer To Store the word d Integer To Count the frequency of Each word