-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdelete_all_repeated_words.java
More file actions
63 lines (61 loc) · 1.65 KB
/
delete_all_repeated_words.java
File metadata and controls
63 lines (61 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Program to Delete All Repeated Words in String
import java.util.*;
class delete_all_repeated_words
{
public static String no_dublicate(String str)
{
String words[]=str.split(" ");
int arr[]=new int[words.length];
for(int i=0;i<words.length;i++)
{
arr[i]=0;
}
for(int i=0;i<words.length;i++)
{
if(arr[i]==0)
{
for(int j=i+1;j<words.length;j++)
{
if(words[i].compareTo(words[j])==0)
{
arr[j]=1;
}
}
}
}
int len=0;
for(int i=0;i<words.length;i++)
{
if(arr[i]==0)
{
len++;
}
}
String nword[]=new String[len];
int j=0;
for(int i=0;i<words.length;i++)
{
if(arr[i]==0)
{
nword[j]=words[i];
j++;
//System.out.println(words[i]);
}
}
StringBuffer sb=new StringBuffer();
for(int i=0;i<nword.length;i++)
{
sb.append(nword[i]);
sb.append(" ");
}
String nstr=sb.toString();
return nstr;
}
public static void main(String arr[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the String :");
String str=new String(sc.nextLine());
System.out.println("The String without dublicate is :"+no_dublicate(str));
}
}