-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringFilter.java
More file actions
110 lines (106 loc) · 2.83 KB
/
Copy pathStringFilter.java
File metadata and controls
110 lines (106 loc) · 2.83 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
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
109
110
// package lab5;
import java.util.Scanner;
interface Filter
{
boolean Accept(String x);
}
public class StringFilter
{
public static String[] filtering(String[] a, Filter f)
{
int len = a.length, k=0;
boolean ans;
String[] filt=new String[len];
for(int i=0;i<len;i++)
{
System.out.println("Checking: "+a[i]);
ans = f.Accept(a[i]);
if(ans==true)
{
filt[k++]=a[i];
}
}
return filt;
}
public static void main(String args[])
{
Scanner s1 = new Scanner(System.in);
System.out.println("Enter length of String array: ");
int n = s1.nextInt();
String[] ex = new String[n];
System.out.println("Enter Strings:");
for(int i=0;i<n;i++)
{
if(s1.hasNext())
{
ex[i] = s1.nextLine();
}
else
{
System.out.println("You didn't provide enough strings!");
break;
}
}
String[] A = filtering(ex, new Filter()
{
public boolean Accept(String x)
{
System.out.println("A: ");
int len = x.length();
if(len<=3)
return true;
else
return false;
}
}
);
String[] B = filtering(ex, new Filter()
{
public boolean Accept(String x)
{
System.out.println("B: ");
int last_index=x.length()-1;
char[] new_x = x.toCharArray();
if(new_x[last_index]=='s')
return true;
else
return false;
}
}
);
String[] C = filtering(ex, new Filter()
{
public boolean Accept(String x)
{
System.out.println("C: ");
int i, last=x.length(), flag=0;
char[] new_x = x.toCharArray();
for(i=0;i<last/2;i++)
{
if(new_x[i]!=new_x[last-i-1])
{
flag=1;
break;
}
}
if(flag==0)
return true;
else
return false;
}
}
);
for(int i=0;i<n;i++)
{
System.out.println(A[i]);
}
for(int i=0;i<n;i++)
{
System.out.println(B[i]);
}
for(int i=0;i<n;i++)
{
System.out.println(C[i]);
}
}
}