The document presents a Java program that identifies and prints duplicate words from a user-input string. It utilizes a Scanner to read the input, processes the string to find words, and checks for repetitions. The program outputs each duplicate word found in the input string.
The document presents a Java program that identifies and prints duplicate words from a user-input string. It utilizes a Scanner to read the input, processes the string to find words, and checks for repetitions. The program outputs each duplicate word found in the input string.
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()+" "; //Store the String and convert it into LowerCase
int e; //To store index of space
for(int i=0;i<s.length();i+=e) //loop till length of string
e = s.indexOf(' ',i); //storing index of Space
String w = s.substring(i,e); //storing word between i and e
int d = s.indexOf(w, e + 1); //to check if word is repeated
if (d>0) //if the word is more than one time in the string
System.out.println(w); //To print the 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