In: Computer Science
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:
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.
The words are provided to stdin either by piping or by user input.
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:
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.
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?
Open the manual page for read() system call and add to your code the header files needed for read().
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.
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.
Open the manual page for close() system call and determine which header file you should include in your code to call close().
Use the close() system call to close the file.
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.