Intro to prog c question!!1 Write in the text box window below a function countI
ID: 3775094 • Letter: I
Question
Intro to prog c question!!1
Write in the text box window below a function countInFile that takes as parameters a file name fname (of type char[]) and a character c and that returns the number of times character c occurs in the file. You need to open the file, then read characters (one by one) in a loop and count, and, finally, close it before returning the count.
The function prototype is:
int countInFile(char fname[], char c);
For example if a file named “text” has the content “all men are created equal”, then calling
countInFile(“text”, ‘e’)
should return 5.
Ensure the code is correct, aligned, and indented properly. Use meaningful variable names.
Explanation / Answer
program
import javax.swing.*;
import java.text.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
public class wordCount extends JFrame implements ActionListener
{
JPanel panel = new JPanel();
JPanel button = new JPanel();
JTextArea main = new JTextArea();
JTextField result = new JTextField(5);
JButton countWords = new JButton("Count Words");
JLabel wordCount = new JLabel("Word Count= ");
public static void main(String[] args) throws IOException
{
String str;
int totalWords = 0;
int whitespaceCount = 0;
int length = 0;
BufferedReader dataIn = new BufferedReader(new InputStreamReader (System.in));
str = dataIn.readLine();
while( str.length() > 0)
{
length = str.length();
for (int i = 0; i < length; i++)
{
if (Character.isWhitespace(str.charAt(i)))
whitespaceCount++;
}
totalWords = length-whitespaceCount;
}
wordCount w = new wordCount();
w.setSize(450,300);
w.setTitle("Word Count");
w.setResizable(false);
w.setVisible(true);
w.setLocation(200,200);
}
public wordCount()
{
Container c = getContentPane();
c.setLayout(new BorderLayout());
panel.setLayout(new GridLayout());
button.setLayout(new FlowLayout(FlowLayout.CENTER));
panel.add(main);
button.add(countWords);
button.add(wordCount);
button.add(result);
c.add(panel, BorderLayout.CENTER);
c.add(button,BorderLayout.SOUTH);
countWords.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.