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

Frequency of Duplicate Words

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.

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)
4 views

Frequency of Duplicate Words

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.

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 frequency of Duplicate words

import java.util.*; //Importing packages

import java.io.*;

import java.lang.*;

public class Frequency_Word //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()+" "; //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

You might also like