Help on SML/NJ homework Consisting of a function that accepts two strings. Each
ID: 3794377 • Letter: H
Question
Help on SML/NJ homework Consisting of a function that accepts two strings. Each string is the name of a file. The first is the name of an input file and the second is the name of an output file. Name the function ‘hw1’.(Note that your program can also make use of other helper functions – just make sure function ‘hw1’ takes as arguments the input file and output file that are specified in the program) A pangram is a sentence that contains all the letters of the English alphabet at least once. For example, the quick brown fox jumps over the lazy dog is a pangram. The program you are to write must read in an input file(input.txt - a plain text file which contains 5 sentences), line by line and check if the line read is a pangram or not. If the sentence read is a pangram, it writes ‘true’ to the output file. If it’s not, it writes ‘false’ to the output file. For example, if input.txt contains: we promptly judged antique ivory buckles for the next prize. how quickly daft jumping zebras vex. pottery is an art. crazy fredrick bought many very exquisite opal jewels. mr.dumbledore is a funny name for a dog. Then your program must output to the output file: true true false true false NOTE : Output is case sensitive – please use all lower case in the output file. You can assume that input.txt contains exactly 5 sentences and all the letters are in lower case. Please use the sample test cases provided to test your code. For the purpose of this assignment you do not need to do any specific error checking on the files. Your program can assume that the files exist (for the input file) or can be created or overwritten (for the output file). Before you start, make sure you are using the following versions: SMLNJ 110.79 You can assume that input.txt contains exactly 5 sentences and all the letters are in lower case. Please use the sample test cases provided to test your code. For the purpose of this assignment you do not need to do any specific error checking on the files. Your program can assume that the files exist (for the input file) or can be created or overwritten (for the output file). Put your answer SML in file named hw1.ml
Explanation / Answer
Please Find the below Java code to check panagrams in an input file and will write to output File.
import java.io.*;
import java.util.*;
public class PangramChecker {
public static void main(String[] args) throws IOException{
FileInputStream fis = null;
BufferedReader input = null;
//Provide your File input path where the input file is present
fis = new FileInputStream("C:\Users\ksumanth\Documents\pangram\input.txt");
input = new BufferedReader(new InputStreamReader(fis));
//Provide your File output path to write the output
FileWriter writer = new FileWriter("C:\Users\ksumanth\Documents\pangram\output.txt");
String line = null;
int lineNo = 1;
//Exactly Five lines are present in the input
while(lineNo <= 5){
line = input.readLine().toLowerCase();
//calls the panagramChecker method to check whether the string is a panagram or not.
if(checkPangram(line)){
writer.write("true"+" ");
}else{
writer.write("false"+" ");
}
lineNo++;
}
//close the FileWriter object
writer.flush();
writer.close();
}
//checkPanagram method to check whether the string is a panagram or not
private static boolean checkPangram(String line) {
//initialize the alphabet array to zeroes
int alphabet[] = new int[26], i,j;
for(i = 0; i<26 ;i++){
alphabet[i] = 0;
}
int length = line.length();
//if a small case alphabet is present at the current character add the count of that character to alphabet array.
for(i = 0;i<length;i++){
if(line.charAt(i) >= 97 && line.charAt(i) <= 122)
alphabet[line.charAt(i) - 97]++;
}
//check the count of number of distinct alphabets present in the string
int count = 0;
for(i = 0;i<26;i++){
if(alphabet[i] >0){
count++;
}
}
//If count = 26 means all alphabets are present in the string
if(count == 26){
return true;
}
//else count != 26 means the string missing some alphabets.
return false;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.