-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalindromeFile2.java
More file actions
80 lines (71 loc) · 2.21 KB
/
Copy pathPalindromeFile2.java
File metadata and controls
80 lines (71 loc) · 2.21 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class PalindromeFile2{
public boolean palindromeChk(String s){
int len=s.length();
int j=len-1,i=0;
while(i<j){
if(s.charAt(i)!=s.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
public static void main(String[] args) throws IOException{
Scanner sc=new Scanner(System.in);
PalindromeFile2 p=new PalindromeFile2();
System.out.println("enter ur string: ");
String s=sc.nextLine();
String[] words=s.split("\\s+");
try(FileWriter fwPal=new FileWriter("First.txt",true);
FileWriter fwNonPal=new FileWriter("Second.txt",true)){
for(String word : words){
boolean isPalindrome=p.palindromeChk(word);
if(isPalindrome){
fwPal.write(word+"\n");
}
else{
fwNonPal.write(word+"\n");
}
}
}catch(IOException e){
System.out.println("error: "+e.getMessage());
}
try(BufferedReader reader=new BufferedReader(new FileReader("Second.txt"))){
String line;
while((line=reader.readLine())!=null){
int[] freq=new int[26];
for(int i=0;i<line.length();i++){
char c=Character.toLowerCase(line.charAt(i));
if(c>='a' && c<='z'){
freq[c-'a']++;
}
}
for(int i=0;i<26;i++){
if(freq[i]>0){
System.out.println((char)(i+'a')+" Repeated "+freq[i]+" times.");
}
}
System.out.println();
}
}
catch(IOException e){
System.out.println("error: "+e.getMessage());
}
}
}
//o,p
// enter ur string:
// sinan wataw rotor food
// a Repeated 1 times.
// i Repeated 1 times.
// n Repeated 2 times.
// s Repeated 1 times.
// d Repeated 1 times.
// f Repeated 1 times.
// o Repeated 2 times.