Question

In C write a text analyzer program that reads any text file. The program displays a...

In C write a text analyzer program that reads any text file. The program displays a menu that gives the user the options of counting: • Lines • Words • Characters • Sentences (one or more word ending with a period) • or all of the above Provide a separate function for each option. At the end of the analysis, write an appropriate report.

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

Solution:

TextAnalyser.c:

#include <stdio.h>
#define FILE_NAME 50
FILE *fp;
void countLines(char filename[]) {
   char c;
   int linecount = 0;
   for (c = getc(fp); c != EOF; c = getc(fp))
if (c == '\n')
linecount = linecount + 1;
printf("\nThe file %s has %d lines", filename, linecount);
}
void countWords(char filename[]) {
   char c;
   int wordcount=1;
   c=fgetc(fp);
while(c!=EOF)
{
if(c==' '||c=='\n')
{
wordcount++;
}
c=fgetc(fp);
}

   printf("\nThe file %s has %d words", filename, wordcount);
}
void countCharacters(char filename[]) {
   char c;
   int charactercount = 0;
   while ((c=getc(fp)) != EOF) {
       if (c != ' ' || c != '\n') {
           charactercount += 1;
   }
   }
printf("\nThe file %s has %d characters", filename, charactercount);
}
void countSentences(char filename[]) {
   char c;
   int sentencecount = 0;
   while ((c=getc(fp)) != EOF) {
       if (c == '.' || c== '!' || c=='?') {
           sentencecount += 1;
   }
   }
printf("\nThe file %s has %d sentences", filename, sentencecount);
}

int main()
{
char filename[FILE_NAME];
printf("Enter file name: ");
scanf("%s", filename);
fp = fopen(filename, "r");
  
// Check if file exists
if (fp == NULL)
{
printf("Could not open file %s", filename);
return 0;
}
int choice;
printf("1.Lines\n2.Words\n3.Characters\n4.Sentences\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
       case 1: countLines(filename);
               break;
       case 2: countWords(filename);
               break;
       case 3: countCharacters(filename);
       break;
       case 4: countSentences(filename);
               break;
       default: printf("\n please enter correct choice!");
               break;
           }
             
// Open the file
  
// Extract characters from file and store in character c
  
  
return 0;
}

Sample1.txt:

Ants are found everywhere in the world. They make their home in buildings, gardens etc. They live in anthills. Ants are very hardworking insects. Throughout the summers they collect food for the winter season. Whenever they find a sweet lying on the floor they stick to the sweet and carry it to their home. Thus, in this way, they clean the floor.Ants are generally red and black in colour. They have two eyes and six legs. They are social insects. They live in groups or colonies. Most ants are scavengers they collect whatever food they can find. They are usually wingless but they develop wings when they reproduce. Their bites are quite painful.

Output:

Add a comment
Know the answer?
Add Answer to:
In C write a text analyzer program that reads any text file. The program displays a...
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
  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • Write a program that opens a specified text file and then displays a list of all...

    Write a program that opens a specified text file and then displays a list of all the unique words found in the file. Hint: Store each word as an element of a set. USING PYTHON Please provide a screenshot of the input

  • in c++ please. Write a program that reads the contents of a text file. The program...

    in c++ please. Write a program that reads the contents of a text file. The program should create a map in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word "the" appears 128 times, the map would contain an element with "the" as the key and 128 as the value. The program should either display the frequency of each word or create...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • Write a complete Python program with prompts for the user for the main text file (checks...

    Write a complete Python program with prompts for the user for the main text file (checks that it exists, and if not, output an error message and stop), for any possible flags (including none), and for any other input that this program may need from the user: split has an option of naming the smaller files head_tail list the first 10 lines (default) and the last 10 lines (default) in order of the given text file flag: -# output #...

  • Write a java program that reads a file (names as textfile) uploaded with assignment and displays...

    Write a java program that reads a file (names as textfile) uploaded with assignment and displays the words of that file as a list. Then display the words in reverse order. Then display them with all plurals (ending in "s") capitalized. Then display them with all plural words removed.

  • write a C++ program that reads in a text file and reads each character or lexemes...

    write a C++ program that reads in a text file and reads each character or lexemes and states what token it is and writes to another file for example: while (fahr < upper) a = 23.00 whileend Output: token lexeme -------------------------------- keyword while separator ( identifier fahr operator < identifier upper separator ) identifier a operator = real 23.00 keyword whileend the program aslo reads in comments but does not add to the list. characters that are commented out or...

  • Write a C program that reads characters from a text file, and recognizes the identifiers in...

    Write a C program that reads characters from a text file, and recognizes the identifiers in the text file. It ignores the rest of the characters. An identifier is a sequence of letters, digits, and underscore characters, where the first character is always a letter or an underscore character. Lower and upper case characters can be part of the identifier. The recognized identifier are copied into the output text. b. Change the C program in exercise 2, so that all...

  • Write a program that reads in a text file, infile.txt, and prints out all the lines...

    Write a program that reads in a text file, infile.txt, and prints out all the lines in the file to the screen until it encounters a line with fewer than 4 characters. Once it finds a short line (one with fewer than 4 characters), the program stops. For your testing you should create a file named infile.txt. Only upload your Python program, I will create my own infile.txt. Please use a while-loop BUT do not use break, Exit or Quit...

  • 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]...

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