Question

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] filename

• The -c flag means to count the number of words in the file. A word would be a series of characters separated by spaces or punctuation. A word could include a hyphen or a single apostrophe.

• The -s option means to print the words in the file sorted by ASCII order.

• The -f option will find the number of occurrences of the given substring.

• You may have any number of the flags included or none of them.

• The order they should be run would be: -s first, -c second, and -f third.

• Output should be well formatted and easy to read.

Please do not copy and paste a code asked in chegg.

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

The C code:

#include<stdio.h>
#include <string.h>
#include<stdlib.h>
#define n 10000
#define OUT 0
#define IN 1

void search_occ(char *word, char *all_str){

int v=0, m=0, times=0, len;
len = strlen(word);
while(all_str[v] != '\0') {
if(all_str[v] == word[m]) {
while(all_str[v] == word[m] && all_str[v] !='\0') {
v++;
m++;
}
if(m == len && (all_str[n] == ' ' || all_str[v] == '\0')) {
times++;
}
} else {
while(all_str[v] != ' ') {
v++;
if(all_str[v] == '\0')
break;
}
}
v++;
m=0;
}
if(times > 0) {
printf("\n'%s' appears %d time(s)\n", word, times);
} else {
printf("\n'%s' does not appear in the sentence.\n", word);
}
}

void all_words(char *str1,int num_words){

int i, j, c = 0;
char temp[50], *split_str, str[num_words][20];
split_str = strtok (str1," ,.-");

while (split_str != NULL){
strcpy(str[c], split_str);
split_str = strtok (NULL, " ,.-");
c++;
}
for(i=1;i<num_words;i++){
for(j=1;j<=num_words-i;j++){
if(strcmp(str[j-1],str[j])>0){
strcpy(temp,str[j-1]);
strcpy(str[j-1],str[j]);
strcpy(str[j],temp);
}
}
}
printf("\n\n Sorted Order \n");
for(i=0; i<10; ++i){
puts(str[i]);
}
printf("\n\n");
}

unsigned countWords(char *str){
int state = OUT;
unsigned wc = 0;
while (*str){
if (*str == ' ' || *str == '\n' || *str == '\t')
state = OUT;
else if (state == OUT){
state = IN;
++wc;
}
++str;
}
return wc;
}

int main(){
char buff[n], search_str[100];
FILE *file;
size_t nread;
int i, num_of_words;

file = fopen("test.txt", "r");
if (file) {
printf("File Content...\n\n");
while ((nread = fread(buff, 1, sizeof buff, file)) > 0)
fwrite(buff, 1, nread, stdout);
if (ferror(file)) {
printf("Error....");
}
fclose(file);
}
num_of_words = countWords(buff);
all_words(buff, num_of_words);
printf("\n\n Number of words-->>%d\n\n", num_of_words);

printf("Enter word to search->");
scanf("%s[^\n]",search_str);

search_occ(search_str, buff);


}

You can run this code and get the desired output.

Important: Before executing the code, create a file name file.txt in that folder only and type the test for which you want to get the desired output.

Add a comment
Know the answer?
Add Answer to:
Write a C program to run on ocelot to read a text file and print it...
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 Python program to read lines of text from a file. For each word (i.e,...

    Write a Python program to read lines of text from a file. For each word (i.e, a group of characters separated by one or more whitespace characters), keep track of how many times that word appears in the file. In the end, print out the top twenty counts and the corresponding words for each count. Print each value and the corresponding words, in alphabetical order, on one line. Print this in reverse sorted order by word count. You can assume...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

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

  • Write a program that inputs a text file. The program should print the unique words in...

    Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Note: The sort function sorts first numbers, then capital letters, then lowercase letters. That is fine for this assignment as you can see from the sample output. But even if the same word appears multiple times in the file it should only be displayed once in the output. (python)

  • I cant get this python program to read all the unique words in a file. It...

    I cant get this python program to read all the unique words in a file. It can only read the number of the unique words in a line. import re import string from collections import Counter # opens user inputted filename ".txt" and (w+) makes new and writes def main(): textname = input("Enter the file to search: ") fh = open(textname, 'r', encoding='utf-8' ) linecount = 0 wordcount = 0 count = {} print("Sumary of the", fh) for line in...

  • Hi, need this question ansered in c++, has multiple levels will post again if you can...

    Hi, need this question ansered in c++, has multiple levels will post again if you can complete every level so keep an eye out for that. here is a sketch of the program from the screenshot int main (int argc, char** argv) { enum { total, unique } mode = total; for (int c; (c = getopt(argc, argv, "tu")) != -1;) { switch(c) { case 't': mode = total; break; case 'u': mode = unique; break; } } argc -=...

  • Creat a C Program that Reads words from a file called words, which contains one word...

    Creat a C Program that Reads words from a file called words, which contains one word per line.Each word has maximum length to M.The number of words in the file is equal to N. Incert each word to Binary Search Tree with node name dictionary.Each node of the tree must contain one word.The left child must contain a word that it is lexicographically smaller while the right child contains a lexicographically bigger one. 1) When you finish reading the file,...

  • Question 2 Write a program that will read in a line of text up to 100...

    Question 2 Write a program that will read in a line of text up to 100 characters as string, and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by whitespace, a period, a comma, or the beginning or end of the line. You can assume that the input consists entirely of letters, whitespace, commas, an<d periods. When...

  • Python 3:Write a program that inputs a text file. The program should print the unique words...

    Python 3:Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order.   Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order An example file along with the correct output is shown below: example.txt the quick brown fox jumps over the lazy dog Enter the input file name: example.txt brown dog fox jumps lazy over quick the

  • Your task is to process a file containing the text of a book available as a...

    Your task is to process a file containing the text of a book available as a file as follows: A function GetGoing(filename) that will take a file name as a parameter. The function will read the contents of the file into a string. Then it prints the number of characters and the number of words in the file. The function also returns the content of the file as a Python list of words in the text file. A function FindMatches(keywordlist,...

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