-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavalab17.java
More file actions
56 lines (52 loc) · 1.48 KB
/
Copy pathjavalab17.java
File metadata and controls
56 lines (52 loc) · 1.48 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
import java.io.*;
import java.util.*;
class palindromecounter
{
public static boolean ispallindrome(String st){
int i=0,j=0;
int n=st.length();
j=n-1;
while(i<j){
if(st.charAt(i)!=st.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
public static int countPalindrome(String filename)
throws IOException{
int count[] = new int[];
try(
BufferedReader br = new BufferedReader(new FileReader(filename))
){
String line;
while((line = br.readLine())!=null){
String words[] = line.replaceAll("[^a-zA-Z\\s]","").toLowerCase().split(" ");
for(String word: words){
if(!word.isEmpty() && ispallindrome(word)){
count[i++]=word;
}
}
}
}
catch (Exception e) {
System.out.println(e.toString());
}
return count;
}
}
public class javalab17 {
public static void main(String[] args) {
String file = "C:\\Users\\Asus\\OneDrive\\Documents\\myFile.txt";
try{
palindromecounter ob = new palindromecounter();
String palincount = ob.countPalindrome(file);
System.out.println("number of pallindrome: " + palincount);
}
catch(IOException e){
System.out.println(e.toString());
}
}
}