Question

Write a program to read in the file below and count the number of sentences, and...

Write a program to read in the file below and count the number of sentences, and the number of each sentence terminators noted in the next statement. A sentence ends with a '.', ';', '?', or '!'.

The name of the file should be a command line parameter to your program called terminatorCounters.

Just a little confused on how to get started here.

the file is just a text file.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*terminatorCounters.c*/
#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp;
char *filename;
char ch;
int line_count = 1;
int terminator_count =0;
  
//Check if the argument has Filename
if (argc < 2)
{
printf("No file name in the command line argument");
return(0);
}
  
filename = argv[1];
printf("Name of the file: %s\n", filename);
  
// Opening the file in read only mode
fp = fopen(filename,"r");

// If file opened successfully, extract characters from file and store in character ch
if ( fp )
{
for ( (ch = fgetc(fp)); ch != EOF ; ch=getc(fp))
{
//Counting number of sentences
if (ch == '\n'){
line_count = line_count + 1;
}
//Counting number of sentence terminator
if ((ch == '.' || ch == '?' || ch == ';' || ch == '!')){
terminator_count = terminator_count + 1;
}
}
  
printf("Total number of sentences %d \n ", line_count);
printf("Total number of terminator characters %d \n ", terminator_count);
}
else
{
printf("Unable to open the file\n");
}
// Close the file
fclose(fp);
  
return(1);
}

Add a comment
Know the answer?
Add Answer to:
Write a program to read in the file below and count the number of sentences, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • Write a C program to run on ocelot to read a text file and print it...

    Write a C program to run on ocelot to read a text file and print it to the display. It should optionally find the count of the number of words in the file, and/or find the number of occurrences of a substring, and/or take all the words in the string and sort them lexicographically (ASCII order). You must use getopt to parse the command line. There is no user input while this program is running. Usage: mywords [-cs] [-f substring]...

  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • Introduction: In this lab, you will write a MIPS program to read in (up to) 50...

    Introduction: In this lab, you will write a MIPS program to read in (up to) 50 integer values from the user, store them in an array, print out the amay, one number per line, reverse the elements in the array and finally print out the elements in the just-reversed) array. Feel free to do this lab and all assembly programming labs) in Windows. You must use MARS Getting started: l. In MARS, create a new assembly file with the name...

  • Write a C function to count the number of sentences entered; assume a sentence ends in...

    Write a C function to count the number of sentences entered; assume a sentence ends in either a period, question mark, or exclamation point.The primary function should be named "sentences" and the secondary function "sentence_counter."

  • I am writing a program in C++, which requires me to read an input text file...

    I am writing a program in C++, which requires me to read an input text file using command line argument. However, I am using xcode on my Macbook to write C++ program, and use terminal instead of command. How do you use int main(int argc, char** argv[]) to read an input file. My professor requires us not to hard code the text file name like .open("example.txt"); Thank you!

  • Write a program in C that takes a file name as the only argument on the...

    Write a program in C that takes a file name as the only argument on the command line, and prints out the number of 0-bits and 1-bits in the file int main ( int argc , char * argv [] ) { // Check if the user gave an argument , otherwise print " ERROR : no argument " // Check if the file can be read , otherwise print " ERROR : can ’t read " // Otherwise ,...

  • Please help with this python assignment. Thank you. question 1 Write a Python program to read...

    Please help with this python assignment. Thank you. question 1 Write a Python program to read a file line by line store it into a variable. question 2 Write a Python program to read a file line by line store it into an array. question 3 Write a python program to find the longest words. question 4 Write a Python program to count the number of lines in a text file. question 5 Write a Python program to count the...

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT