Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in C Command-Line Arguments Read in the command-line arguments and verify that t

ID: 3886570 • Letter: I

Question

in C

Command-Line Arguments Read in the command-line arguments and verify that there are three parameters: (1) the program name, (2) an input file that is the name of the file you will be working with, and (3) the word pattern that you are searching for. If the number of parameters is incorrect, print out a usage message and terminate the program. File I/O Open the file that was given as a command-line argument to your program. Then, read through the file and print the following information: (1) the total number of lines in the file, (2) the number of characters in the longest line of the file, and (3) the longest line of the file. Pattern Matching (i.e., grep) Create a data structure that holds all of the information for each occurrence of a word pattern. For example, it should hold the line number and the contents of the line it occurs on. Print all of the information (i.e., the line number and the contents of the line it occurs on), including the total number occurrences of the word pattern. Some assumptions and general rules: Your program should be case sensitive (i.e., the word pattern "and" is different than the word pattern "AND"). You may assume that no lines in the file will be longer than 80 characters (81 with the null character). You may assume that the lengths of the filename and word pattern given as command-line arguments are not longer than 15 characters (16 with the null character). You may not make any assumptions about how many occurrences of the word pattern are in the file, or each line for that matter. Therefore, you may want to use a linked list and allocate memory for each occurrence as you go. You must deallocate the memory when no longer needed. Add error checking where needed (e.g., after opening file, allocating memory etc.) and take the appropriate action. If you have any questions about the functionality, please contact your instructor.

Explanation / Answer

#include<stdio.h>
#include<string.h>

int countOccurences(char *str, char *sub){
    int l1,l2,count,count1,i,j;

   
    l1 = strlen(str);
    l2 = strlen(sub);

    for (i = 0; i < l1;)
    {
        j = 0;
        count = 0;
        while ((str[i] == sub[j]))
        {
            count++;
            i++;
            j++;
        }
        if (count == l2)
        {
            count1++;                                  
            count = 0;
        }
        else
            i++;
    }   

    return count1;
}

int main(int argc, char **argv){

    FILE *fp;
    char *word;
    int i,num, total_count, count, charcount;
    int line_count;
    char line[1000];
    char longline[1000];

   
    if (argc != 3){
        printf("Incorrect number of command line argumnets ");
        return 0;
    }
    word = argv[2];
    if ((fp = fopen(argv[1], "r")) == NULL)
    {
        printf("Error! opening file");
        return 0;        
    }
    total_count = 0;
    line_count = 0;
    num = 0;
    printf("%s occurred in following lines ",word);
    while (fgets(line, sizeof(line), fp)){

        charcount = 0;
        for(i=0; line[i]!=' '; i++){
           if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z'))
              charcount++;
        }
       
        if (charcount > num){
            num = charcount;
            for (i=0; line[i]!=' '; i++)
                longline[i] = line[i];
            longline[i] = ' ';
        }


        count = countOccurences(line,word);

        line_count = line_count + 1;
        if (count > 0){
           //printf(line);
           total_count = total_count + count;
        }

       
    }
    printf("Total number of lines : %d ",line_count);
    printf("Longest line : %s ",longline);
    printf("Number of characters in longest line : %d ",num);
    printf("Total number of occurence of word %s : %d ",word,total_count);


          
}