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

  • write a c++ program that reads some numbers from a text file then it displays how...

    write a c++ program that reads some numbers from a text file then it displays how many of these numbers composed of the same digit For example(444 22  232 78 1111) so the program should displays 3 (using only while loops and if statment)

  • With basic (do not use #include <algorithm>, etc.) and simple C++ Write a program which reads...

    With basic (do not use #include <algorithm>, etc.) and simple C++ Write a program which reads a text file “input.txt” and stores all the distinct words in an array. A word consists of letters only - uppercase and/or lowercase. An incoming word should be inserted into the array such that it is always in ascending order. Use binary search to ensure that no duplicate words are added. Assume that there are no more than 100 distinct words. Assume that the...

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

  • In Python, write a program that reads a text file that is provided by the user...

    In Python, write a program that reads a text file that is provided by the user - ensure that the file exists before processing it! The file might be in a different directory, so be sure to test you logic - the user will provide the absolute path to the file if it is not in the same directory as the Python script! You should then ask the user for what string to search for in the file. Your program...

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