Question

In: Computer Science

only using C (not C++) Implement a program that counts the words and prints their frequencies....

only using C (not C++)

Implement a program that counts the words and prints their frequencies. In particular, the program extracts “words” from one or more text files, from a pipe, or from the console, and prints to the console the list of words found and their associated frequencies. Note that your project submission is restricted to only using the following system calls: open(), close(), read(), write(), and lseek() for performing I/O. You are allowed to use other C library calls (e.g., malloc(), free()), however, all I/O is restricted to the Linux kernel’s direct API support for I/O.

There are three ways the list of words is provided to your program:

  1. In a list of one or more files. The names of the files are provided as arguments to your program (i.e., they are copied by the shell in the argv variable), and each file is a text file with lots of words.

  2. The words are provided to stdin either by piping or by user input.

  3. An environment variable, WORD_FREAK, is set with the name of the file containing the words.

We’ll focus on one of these ways, namely, getting the input from a file. Let’s assume the words are provided in one file, passed as an argument to your program (i.e., option 1). A sample text file (for this use any text file with thousands of words) is (not) provided. Given the constraint of using Linux API for I/O, complete the following:

  1. Open the manual page for open() system call and add to your code the header files required to use open(), using the include preprocessor statement.

  2. Use the open() system call to open the file passed to your program in argv[1] for reading. What flag should you pass to open(), to open the file in read-only mode?

  3. Open the manual page for read() system call and add to your code the header files needed for read().

  4. Use the read() system call to read the characters from the file. What parameters does read() take, and what value does it return? Repeat, if necessary the call to read() until all characters are read from the input file.

  5. Use printf() to output to the terminal the text just read from the input file. Note that for the project you are not allowed to use printf(). However, while debugging your code, it can be used to confirm the correct reading of the file contents.

  6. Open the manual page for close() system call and determine which header file you should include in your code to call close().

  7. Use the close() system call to close the file.

Solutions

Expert Solution

C++ Code:-

#include <iostream>
#include <string.h>
#include <fstream>

using namespace std;

//structure defintion
struct WordCount
{
   //two data members
   char word[31];
   int count;
};


//Function to convert string upper case
void stringToUpper(char *s)
{
   for(int i=0;s[i]!='\0';i++) //repeat until end of string char null
   {
       if(*(s+i)>=97 && *(s+i)<=122) //when lower case letter
           *(s+i)=*(s+i)-32;//convert to upper case by subtraction of ascii with 32
   }
}

//Function striping punctuation characters
void stripPunctuation(char *s)
{
   int pos=0;
   for (char *p = s; *p; ++p) //repeat all characters
       if (isalpha(*p)) //when alphabet
           s[pos++] = *p; //then concate to s
   s[pos] = '\0'; //end with null
}
          

//Function search word
int searchForWord(const char* word, const WordCount wordArray[],int numWords)
{
   for(int i=0;i<numWords;i++) //repeat loop
       if(strcmp(wordArray[i].word,word)==0) //when matched
           return i;//return its index
   return -1;//return -1, when fails
}

//Function for counting words
int countWords(const char* fileName,WordCount wordArray[])
{
   ifstream infile(fileName);//open file for reading
   int count=0;//initially count is 0
   char word[31];//take max length word
   infile>>word;//read the word
  
   while(!infile.eof()) //repeat until end of file
   {
       stringToUpper(word);//convert to upper case
       stripPunctuation(word);//remove punctuation characters
       int found=searchForWord(word, wordArray,count);//search word
       if(found==-1) //when not found
       {
           strcpy(wordArray[count].word,word);//store as new word
           wordArray[count].count=1;//first count is 1
           count++;//increment count
       }
       else //when found
           wordArray[found].count+=1;//increment existing count
       infile>>word;//read the next words
   }
   infile.close();//close the file
   return count;//finally return count      
      
}

//Function to sort words
void sortWords(WordCount wordArray[],int numWords)
{
   for(int i=0;i<numWords-1;i++) //outer loop
       for(int j=0;j<numWords-i-1;j++) //inner loop
       {
           if(strcmp(wordArray[j].word,wordArray[j+1].word)==0) //compare
           {
               //swapping process
               WordCount temp=wordArray[j];
               wordArray[j]=wordArray[j+1];
               wordArray[j+1]=temp;
           }
       }
}

//Function prints all words and their frequency
void printWords(const WordCount wordArray[],int numWords)
{
   cout<<"Word\tFrequency"<<endl;
   for(int i=0;i<numWords;i++) //repeat loop
   {
       cout<<wordArray[i].word<<"\t"<<wordArray[i].count<<endl;
   }
}
          

//main with command line arguments
int main(int argc,char* argv[])
{
   WordCount wordcount[200];//declare maximum 200 words
   if(argc!=2) //when count of args are not 2
   {
       cout<<"\nUsage : assign [file-name]"<<endl;//print error message
       return 1; //return error code 1
   }
   else //when passed exactly two args
   {
       int total=countWords(argv[1],wordcount);//read to array
       printWords(wordcount,total);//call print function
   }
      
   return 0;
}

//Sample.txt

I would love to try or hear the sample audio your app can produce. I do not want to purchase because I've purchased so many apps that say they do something and do not deliver,

Can you please add audio samples with text you've converted? I'd love to see the end results.

Thanks!

Output:-

Thank You....!!!!
For any query, please comment.
If you are satisfied by the above answer, then please thumbs up or upvote the answer.


Related Solutions

Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and...
Write a C++ program that prints a calendar for a given year. ONLY USING "#include<iostream>" and "#include<cmath>" The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:       0 Sunday                     1 Monday                   2 Tuesday                   3 Wednesday       4 Thursday                 5 Friday                      6 Saturday Your program should...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions...
Write a C++ program (using pointers and dynamic memory allocation only) to implement the following functions and call it from the main function. (1)Write a function whose signature looks like (char*, char) which returns true if the 1st parameter cstring contains the 2nd parameter char, or false otherwise. (2)Create an array of Planets. Populate the array and print the contents of the array using the pointer notation instead of the subscripts.
Write a C code program to implement the comparisons of three integer numbers, using only conditional...
Write a C code program to implement the comparisons of three integer numbers, using only conditional operators (no if statements). Please ask the user to input random three integers. Then display the minimum, middle, and maximum number in one line. Also, please calculate and display the sum and the average value (save two digits after decimal point) of these three integers. Please write the comments line by line.
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of...
C++ // Program Description: This program accepts three 3-letter words and prints out the reverse of each word A main(. . . ) function and the use of std::cin and std::cout to read in data and write out data as described below. Variables to hold the data read in using std::cin and a return statement. #include <iostream > int main(int argc, char *argv[]) { .... your code goes here }//main Example usage: >A01.exe Enter three 3-letter space separated words, then...
I need a C++ program using while loops that counts the number of characters in a...
I need a C++ program using while loops that counts the number of characters in a sentence. The user inputs a sentence and then terminates the input with either '.' or '!'. And then it needs to count and display the number of a's, e's, i's, o's, u's, and consonants. The program should read both lower and upper case. They don't want us using switch statements or string operators, and want us to us if else if statements. I have...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
Write a program in C++ that prints out the even numbers between 1 and 21 using...
Write a program in C++ that prints out the even numbers between 1 and 21 using WHILE loop. Also, find the sum AND product of these numbers and display the resulting sum and product.
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Important! Consider which control structures will work best for which aspect of the assignment. For example, which would be the best to use for a menu?...
C Program: Create a C program that prints a menu and takes user choices as input....
C Program: Create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. The specifications must be followed exactly, or else the input given in the script file may not match with the expected output. Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT