Coding in Java and please do not use startwith() opertation. Read the whole inpu
ID: 3888175 • Letter: C
Question
Coding in Java and please do not use startwith() opertation.
Read the whole input one line at a time and output each line if it is not a prefix of some previous line. (One string s is a prefix of another string t if t can be written as t=sx for some string x. For example, s='help' is a prefix of t='helpful' because 'helpful' = 'help' + 'ful'). Take care not to waste memory, so that your code stores the fewest number of lines possible. Hint: Consider what happens when you compare 'help' and 'helpful' using the usual ordering on Strings.
Explanation / Answer
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class CompareTextFiles
{
static boolean isSubSequence(String str1, String str2, int m, int n)
{
// Base Cases
if (m == 0)
return true;
if (n == 0)
return false;
// If 1 lines is prefixes of 2 or not
if (str1.charAt(m-1) == str2.charAt(1))
return isSubSequence(str1, str2, m-1, 1);
// If last characters are not matching
return isSubSequence(str1, str2, m, 1);
}
public static void main(String[] args) throws IOException
{
BufferedReader reader1 = new BufferedReader(new FileReader("C:\file1.txt"));
BufferedReader reader2 = new BufferedReader(new FileReader("C:\file2.txt"));
String str1 = reader1.readLine();
String str2 = reader2.readLine();
m=str1.length();
n=str2.length();
boolean res = isSubSequence(str1, str2, m, n);
if(res)
System.out.println("Yes files have lines that are prefixes");
else
System.out.println("Yes files do not have lines that are prefixes");
int lineNum = 1;
reader1.close();
reader2.close();
}
}