-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcs.java
More file actions
124 lines (117 loc) · 3.87 KB
/
Copy pathlcs.java
File metadata and controls
124 lines (117 loc) · 3.87 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.util.*;
import java.io.*;
interface longeststring
{
public String Char(String s);
public int commonstring(String s1,String s2);
}
class ValidInputs implements longeststring
{ //class to take only lowercase characters and spaces
public String Char(String s){
String st=""; // empty string
for (int i=0;i<s.length();i++ ) {
if(95<=s.charAt(i) && s.charAt(i)<=122 || s.charAt(i)==32){
st=st+s.charAt(i); // appending lowercases to string st
}
}
return st; //returning string
}
public int commonstring(String s1,String s2){ //func'n to compare two strings
int lcs=0;
for (int i=0;i<s1.length();i++ ) {
for (int j=0;j<s2.length() ;j++ ) {
String st="";
int y=0;
while(i+y<s1.length() && j+y<s2.length() && s1.charAt(i+y)==s2.charAt(j+y))
{ //incrementing by y and checking the characters or equal not
st=st+s1.charAt(i); // appending the characters if they are same
y++;
}
if(st.length()>lcs) //compare the lengths with lcs
{
lcs=st.length(); //assign the maximum value to lcs
}
}
}
return lcs; //returning maxm length
}
}
class lcs
{
public static String Fileread(String s)throws FileNotFoundException
{
int c=0;
File file = new File(s);
String s1="";
try // To catch error we use try catch method
{
Scanner sc= new Scanner(file);
while(sc.hasNextLine())
{
s1+=sc.nextLine();
s1=s1.replace("\n"," "); // To read n lines in the given file
c=c+1;
}
sc.close();
}
catch(Exception e)
{
e.printStackTrace(); // it prints the error
}
return s1;
}
public static void main(String[] args) throws FileNotFoundException
{
File folder=new File(args[0]);// Craeting folder to file path
int filescount=0;
File[] listoffiles=folder.listFiles(); //Creating a listoffile array and copy files
// it from folder
File[] file_name=new File[listoffiles.length];//Creating a String array
for (int i=0;i<listoffiles.length ;++i )
{ // and entering the files in it
File file=listoffiles[i];
if(file.getName().endsWith(".txt"))
{ // condition to have only .txt files
file_name[filescount]=file;
filescount++;
}
}
if(filescount==0)
{
System.out.println(" empty directory"); //if there are no files, print it
}
for (int i=0;i<filescount;++i )
{
System.out.print(" "+file_name[i].getName()); // For Matrix printing in columns
}
System.out.println("\n");
for (int i=0;i<filescount;++i )
{
System.out.print(file_name[i].getName()); //For matrix printing in row
for (int j=0;j<filescount;++j)
{
if(i==j)
{
System.out.printf("\t"+"%.2f",100.00);
}
else
{
ValidInputs l=new ValidInputs();
String s3=Fileread(file_name[i].getName()); //calling the function fileopen and assigning it to string
String s4=Fileread(file_name[j].getName());
String s1=l.Char(s3); //calling the function char and removing special characters
String s2=l.Char(s4);
int denominator1=s1.length();
int denominator2=s2.length();
int denominator=denominator1+denominator2;
int numerator=l.commonstring(s1,s2); //function to find out the maximum length of the common string
if(numerator*2==denominator)
System.out.printf("\t"+"%.2f",100.00);
else
System.out.printf("\t"+"%.2f",(float)(numerator*200)/denominator);
}
} //calculating the percentage
System.out.println();
}
}
}