Question

I need a program in C that counts the number of the occurences of 6 specific...

I need a program in C that counts the number of the occurences of 6 specific words (house, person, dog, door, long, mat) from a file. It should read the input file and print out how many of these strings occur in the file. The file is arbitrary but might contain something like : blue house person garage person dog door couch long mat house dog boom door person (so there are more words than just the ones I need to count).

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

CODE:

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
FILE *fptr;   
int i, len, index;

// List of distinct words
char words[6][10]={"house", "person", "dog", "door", "long", "mat"};
char word[50];
char *temp;

// Count of distinct words
int count[6];
fptr = fopen("input.txt", "r");

/* Exit if file not opened successfully */
if (fptr == NULL)
{
printf("Unable to open file.\n");
exit(0);
}
// Initialize words count to 0
for (i=0; i<6; i++)
count[i] = 0;   
while (fscanf(fptr, "%s", word) != EOF)
{
// Convert word to lowercase
strlwr(word);
// Remove last punctuation character
len = strlen(word);
if (ispunct(word[len - 1]))
word[len - 1] = '\0';
for(i=0;i<6;i++)
{
temp=words[i];
if((strcmp(temp,word))==0)
count[i]++;
}
}   
fclose(fptr);
//Print occurrences of all words in file.
printf("\nOccurrences of all words in file: \n");
for (i=0; i<6; i++)
{
printf("%-15s => %d\n", words[i], count[i]);
}
return 0;
}

SCREENSHOTS:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main() FILE *fptr: int i, len, index: // Lilen = strlen(word); if (ispunct (word[len - 1])) word[len - 1] = \0; for(i=0;i<6;i++) temp=words[i] || if((strcmp (temp, woC:\Program Files (x86)\Dev- all words in file: Occurrences of house person dog door long mat 111111 Process exited normally.

Note: Please Give Me Up Vote Thank You.

Add a comment
Know the answer?
Add Answer to:
I need a program in C that counts the number of the occurences of 6 specific...
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
  • Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words...

    Word count. A common utility on Unix/Linux systems. This program counts the number of lines, words (strings of characters separated by blanks or new lines), and characters in a file (not including blank spaces between words). Write your own version of this program. You will need to create a text file with a number of lines/sentences. The program should accept a filename (of your text file) from the user and then print three numbers: The count of lines/sentences The count...

  • I need this written in the C format not C++ Name this program count.c-The program reads...

    I need this written in the C format not C++ Name this program count.c-The program reads an unknown number of words - strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. 4.

  • Hello! i need a program in C that uses FILE*! The instructions are below! Please dont...

    Hello! i need a program in C that uses FILE*! The instructions are below! Please dont copy paste another chegg post! Thank you! Will leave positive review! I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each...

  • The program reads an unknown number of words – strings that all 20 characters or less...

    The program reads an unknown number of words – strings that all 20 characters or less in length. It simply counts the number of words read. The end of input is signaled when the user enters control-d (end-of-file). Your program prints the number of words that the user entered. ****** How do you I make it stop when control-d is entered. My code: #include <stdio.h> void main(void) { char sentence[100]; int i = 0; int count = 1; printf("Enter a...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Hello I need a small fix in my program. I need to display the youngest student...

    Hello I need a small fix in my program. I need to display the youngest student and the average age of all of the students. It is not working Thanks. #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <algorithm> using namespace std; struct Student { string firstName; char middleName; string lastName; char collegeCode; int locCode; int seqCode; int age; }; struct sort_by_age { inline bool operator() (const Student& s1, const Student& s2) { return (s1.age < s2.age); // sort...

  • I need help with a c++ function. I need this function to read in an input...

    I need help with a c++ function. I need this function to read in an input file containing multiple choice questions/potential answers and to store that data into a 2d array of strings. This function cannot use classes or vectors or anything "advanced". The input file will have 0-50 questions; each with 4 multiple choice responses. The array should be titled questionArray. (it's a .txt file) update: What is my name? John Bob Lance Mike What is my favorite animal?...

  • c program that counts the number of characters, words and lines from standard input until EOF....

    c program that counts the number of characters, words and lines from standard input until EOF. attached is what i Have so far but its not working ?. about shell redirection Requirements 1. Write a C program that counts the number of characters, words and lines read from standard Input until EOF Is reached. 2. Assume the Input is ASCII text of any length. 3. Every byte read from stdin counts as a character except EOF 4. Words are defined...

  • IN C++ Write a program in c++ that will read from a file the following sentence:...

    IN C++ Write a program in c++ that will read from a file the following sentence: The quick brown fox jumps over the lazy dog Each word must be read into one location in an array beginning with the first element. You must declare an array as follows: char *words [9] ; // this is an array of c- strings. HINT words[0] will contain "the" words[1] will contain "quick" write a function int length (const char *a) to determine the...

  • I have the current code that is working. I need to edit it so that I...

    I have the current code that is working. I need to edit it so that I am only counting strings that are after the word 'From' in a text file. Right now it's counting all words, but I just need the From email addresses in a text file. I think the issue if in the if/else loop but nothing I have tried works. Please help! 1 import string 2 fhand open('C: \Users \Brooke\Canopy scripts mbox-short.txt') 3 counts dict( 4 for...

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