0% found this document useful (0 votes)
2 views

Duplicate

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.

Uploaded by

98hwkb4rzt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Duplicate

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.

Uploaded by

98hwkb4rzt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Program to print Duplicate words in the String

import java.util.*; //Importing packages

import java.io.*;

import java.lang.*;

public class Duplicate //declaring class

{ //opening braces of class

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

You might also like