In this tutorial you will learn about the Duplicate Words in String Program in Java and its application with practical example.
In this tutorial, we will learn to create a Duplicate Words in String Program in Java using java programming.
Prerequisites
Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
Duplicate Words in a String
Here we need to find out the duplicate words present in a string and display those words.
Example string = ” Hi, welcome to W3adda hi”
Output: “Hi”
Duplicate Words in String Program in Java
Output
Duplicate Word
In the above program, we have first declared and initialized a set of variables required in the program.
- str= it will hold the string
- length= it will hold the length of a string
- i = for iteration
- wd = it will hold the duplicate words
After declaring variables we initiate values in str .
after initiating string in the variable str first of all we convert into lower case.
then we split a string into words by using the split() method.
then we count the length of a string
and within the loop we find duplicate word present in a string as shown mage below
And finally, we will print the duplicate values within the string.