write a program that will open the file, count the number of occurences of each word in the the complete works of shakespeare. 
You will then tabulate this information in another file.



 write a program that will open the file, count the number of occurences of each word in the th..

Answer / Awashesh Kumar Tiwari

Here's an example C program to count the occurrence of each word in Shakespeare's works and save the results in a file:

```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORDS 10000
#define BUFFER_SIZE 256

struct WordCount {
char word[BUFFER_SIZE];
int count;
};

void readFile(const char *filename, struct WordCount words[], int maxWords) {
FILE *file = fopen(filename, "r");
if (file == NULL) return;

char line[BUFFER_SIZE];
char word[BUFFER_SIZE];
int index = 0;
while (fgets(line, BUFFER_SIZE, file)) {
char *token = strtok(line, " ,.n");
while (token != NULL) {
// Remove punctuation and convert to lowercase
int len = strlen(token);
for (int i = 0; i < len; ++i) {
if (!isalpha((unsigned char)token[i])) token[i] = '';
}
token[len] = '';

// Find the word in the word count array and increment its count, or add it if not found yet
int i;
for (i = 0; i < index; ++i) {
if (!strcmp(words[i].word, token)) {
words[i].count++;
break;
}
}
if (i == index) {
strcpy(words[index++].word, token);
words[index - 1].count = 1;
if (index >= maxWords) break;
}

token = strtok(NULL, " ,.n");
}
}
fclose(file);
}

void writeFile(const char *filename, const struct WordCount words[], int numWords) {
FILE *file = fopen(filename, "w");
if (file != NULL) {
for (int i = 0; i < numWords; ++i) {
fprintf(file, "%s: %dn", words[i].word, words[i].count);
}
fclose(file);
}
}

int main(void) {
const char *filename = "shakespeare.txt";
struct WordCount words[MAX_WORDS];
readFile(filename, words, MAX_WORDS);
writeFile("word_counts.txt", words, indexOfWord(words, ""));
return 0;
}

int indexOfWord(struct WordCount words[], const char *target) {
for (int i = 0; i < MAX_WORDS; ++i) {
if (!strcmp(words[i].word, target)) return i;
}
return -1;
}
```
This program reads a text file containing Shakespeare's works, counts the occurrence of each word, and saves the results in another file.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

How are Structure passing and returning implemented by the complier?

1 Answers   TISL,


consagous technology placement paper

3 Answers   Consagous, Sypra,


Write c-code for 5+55+555+5555+55555+555555+5555555. Output will be it's answer...

4 Answers   TCS,


What is the benefit of using #define to declare a constant?

1 Answers  


What is the most efficient way to count the number of bits which are set in a value?

4 Answers  


what is the syallabus of computer science students in group- 1?

1 Answers  


What is the diffrent between while and do while statement ?

6 Answers  


If i have an array 0 to 99 i.e,(Size 100) I place the values 1 to 100 randomly like a[0]=29,a[1]=56 upto array[99].. the values are only between 1 to 100. getting the array values by using scanf.. If i entered one wrong element value line a[56]=108. how can i find it.. and also how to find the missing value in 1 to 100.. and i want to replace the missing values.. any one of them know please post your answer..

1 Answers   IBM, Wipro,


who developed c and why he developed c?

5 Answers  


we called a function and passed something do it we have always passed the "values" of variables to the called function. such functions calles are called a) calls by reference b) calls by value c) calls by zero d) none of the above

1 Answers  


What is the restrict keyword in C?

2 Answers  


What is difference between union and structure in c?

1 Answers  


Categories