In: Computer Science
Write a C program to run on unix to read a text file and print it to the display. It should count of the number of words in the file, find the number of occurrences of a substring, and take all the words in the string and sort them (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.
--------------------------------------------------------------------------------
Format of parsing the command line (main method established), rest of functionality needed as explained above:
int value = 0; int main(int argc, char **argv) { extern char *optarg; extern int optind; int c, err = 0; int cflag = 0, sflag = 0, fflag = 0; char *substring; // usage static char usage[] = "usage: mywords [-cs] [-f substring] filename"; while ((c = getopt(argc, argv, "csf:")) != -1) switch (c) { case 'c': cflag = 1; break; case 's': sflag = 1; break; case 'f': fflag = 1; substring = optarg; break; case '?': err = 1; break; } if (fflag == 1) { printf("%c ", substring); } }
Updated Answer
Here we have written 4 functions :
1) printSortedWords
2)countWords
3) findSubString
4) readFile
for the various functionalities. Refer to the comments for the logic used. The main function checks for various flags and calls the respective functions. Here we have read the file character by character .
#include <stdio.h>
#include<string.h>
#include<stdio.h>
char * readFile(char *fileName);
int main(int argc, char **argv) {
extern char *optarg;
extern int optind;
int c, err = 0;
int cflag = 0, sflag = 0, fflag = 0;
char *substring;
// usage
static char usage[] = "usage: mywords [-cs] [-f substring]
filename";
while ((c = getopt(argc, argv, "csf:")) != -1)
switch (c) {
case 'c':
cflag = 1;
break;
case 's':
sflag = 1;
break;
case 'f':
fflag = 1;
substring = optarg;
break;
case '?':
err = 1;
break;
}
if (fflag == 1) {
printf("%s substring ", substring);
}
char filename[25];
*filename=argv[optind]; //getting filename
char target_str[1000];
*target_str=readFile(filename);
if(sflag==1){
printSortedWords(target_str);
}
if(cflag==1){
countWords(target_str);
}
if(fflag==1){
findSubString(target_str,substring);
}
}
//function to read file
char * readFile(char *fileName)
{
FILE *file;
char *str = malloc(1000 * sizeof(char)); //assumed maximum size of
file 1000 bytes
file = fopen(fileName, "r");
do
{
*str++ = (char)fgetc(file);
} while(*str != EOF);
return str;
}
//function to count number of words and print
void countWords(char *s){
int count=0;
for (int i = 0;s[i] != '\0';i++){
if (s[i] == ' ' || s[i]== ',' || s[i] ==';' || s[i]=='?' || s[i]=='\n' ) /* Checking for each punctuation */
{count++;
if(s[i+1]==' ')
i++;
}
}
printf("Count of Words: %d\n", count + 1);/* count +1 to count the last word*/
}
//function to find occurences of substring
void findSubString(char *target_str, char *sub_str){
int l1 = strlen(target_str);
int l2 = strlen(sub_str);
int count1=0;
//length of 2 strings
for (int i = 0; i < l1;)
{
int j = 0;
int k=i;
int count = 0;
while ((target_str[k] == sub_str[j]))//checking character by
character
{
count++;
k++;
j++;
}
if (count == l2)//substring found
{
count1++;
count = 0;
i++;
}
else
i++;//substring not found
}
printf("Number of occurences of %s in %s = %d", sub_str,target_str,
count1);
}
//function to print sorted words
void printSortedWords (char *target_str)
{
int count = 0;
int j = 0;
char s[50][50]; //maximum 50 words of 50 characters each
for (int i = 0; target_str[i] != '\0'; i++)
{
if (target_str[i] == ' ' || target_str[i] == ',' ||
target_str[i] == ';'
|| target_str[i] == '?' || target_str[i] == '\n')
/* Checking for each punctuation and copying each word to different
string */
{
count++;
j = 0;
if (s[i + 1] == ' ')
i++;
}
else
{
s[count][j] = target_str[i];
j++;
}
}
//sorting of Strings
char temp[50];
for (int i = 0; i <= count; i++)
for (j = i + 1; j <= count; j++)
{
if (strcmp (s[i], s[j]) > 0)
{
strcpy (temp, s[i]);
strcpy (s[i], s[j]);
strcpy (s[j], temp);
}
}
printf ("Sorted Words:");
for (int i = 0; i <= count; i++)
puts (s[i]);
}
Sample Input:
-cs -f "dsgdsh" hello.txt
Sample Output: