-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringExample.java
108 lines (102 loc) · 1.9 KB
/
stringExample.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
public class stringExample
{
private String para="This is a repeatable string which repeats This is a repeatable string which repeats This is a repeatable string which repeats";
public void particularFrequency(String s)
{
int index=-1,count=0,i=0;
while(i<para.length())
{
index=para.indexOf(s,i);
if(index>=0)
{
index++;
i=index;
count++;
}
else
{
break;
}
}
System.out.println("\nThe frequency of string |"+s+"| is "+count+"\n");
}
public void frequency()
{
String s[]=para.split(" ");
for(int j=0;j<s.length;j++)
{
int count=0, compare=1;
for(int i=0;i<s.length;i++)
{
compare=s[j].compareTo(s[i]);
if(compare==0)
{
count++;
}
}
if(s[j].equals(""))
{}
else
{
System.out.println("\nThe frequency of the string |"+s[j]+"| is "+count+"\n");
}
String cmp=s[j];
for(int k=0;k<s.length;k++)
{
if(s[k].equals(cmp))
{
s[k]="";
}
}
}
System.out.println();
}
public String myTrim()
{
String s=para.replace(" ", "");
System.out.println("\n"+s+"\n");
return s;
}
public void giveIndex(String s)
{
int index=-1,i=0;
System.out.print("The index of the string |"+s+"| are : ");
while(i<para.length())
{
index=para.indexOf(s,i);
if(index>=0)
{
System.out.print(index+"\t");
index++;
i=index;
}
else
{
break;
}
}
System.out.println("\n");
}
public String[] mySplit(String d)
{
String r=para.replace(d,"["+d+"]@");
String[] s=r.split("@");
for(String i:s)
{
System.out.println(i);
}
return s;
}
}
class TestString
{
public static void main(String...args)
{
stringExample str=new stringExample();
str.myTrim();
str.giveIndex("This");
str.mySplit("is");
str.particularFrequency("is");
str.frequency();
}
}