Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python code Write a function named wordCount that counts how many words there ar

ID: 3831759 • Letter: P

Question

Python code

Write a function named wordCount that counts how many words there are in each line of an input file and writes that count to a corresponding line of an output file. The input file already exists when wordCount is called. wordCount creates the output file. Input. The function wordCount takes two parameters: i. inFile, a string that is the name of a text file that is to be read and analyzed. The file that inFile refers to contains only upper and lower case letters and white space (no punctuation marks or other special characters). ii. outFile, a string that is the name of the file to which wordCount writes its output. The input file is in the current working directory and you should create the output file to that directory. Output. For each line of inFile, wordCount should write a corresponding line to outFile containing a single integer: the number of words on the line. If the content of the file catInTheHat.txt is below, The sun did not shine It was too wet to play So we sat in the house All that cold wet day I sat there with Sally We sat there we two And I said How I wish We had something to do the function call wordCount('catInTheHat.txt', 'catInTheHatOut.txt')

should create a file named catInTheHatOut.txt with this content:

5

6

6

6

5

5

6

5

Explanation / Answer


import java.io.*;
public class fileoperation {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       String line="";
       int len=0;
       char s='s';
       try{
           BufferedReader br=new BufferedReader(new FileReader(new File("cheggin.txt")));
           BufferedWriter bw=new BufferedWriter(new FileWriter(new File("cheggou.txt")));
           while((line=br.readLine())!=null){
               String ar[]=line.split(" ");
               len=ar.length;
               s=(char)len;
               bw.append(len+" ");
bw.newLine();

              
           }
           br.close();
           bw.close();
       }catch(Exception e){
           System.out.println(e);
       }
   }
}


cheggin.txt
kdkkd dksksk sksks sskskks sksks
kssksk
sksks
kskskksksks ksksksksks


output
cheggou.txt
5
1
1
2