Wednesday 8 February 2017

Implement wc command in c


wc command of linux show 3 things
1. Number of Lines In the File
2. Number of Words
3. Number of characters

1. number of lines means we have to count new line characters in file


/*Finding Number of new line characters in a buffer*/
int find_count_of_lines(const char *str){
        int liness=0, i=0;
        for(i=0; str[i] != '\0'; i++){
                if( str[i] == '\n' ){
                        liness+=1;
                }
        }
        return liness;
}

2. number of words means we have to count white space characters, but if multiple white space characters will be there consecutively then we have to count then as one


/* Number of Words in a buffer*/
int find_count_of_words(const char *str){
        int words=0, i=0;
        // skip initial white space characters
        while(str[i] != '\0'){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                        i++;
                }else{
                        break;
                }
        }
        for(; str[i] != '\0'; i++){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t' ){
                        words+=1;
                        // skip consucative white space
                        while(str[i]==' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                                i++;
                        }
                }
        }
        return words;
}

3. number of bytes is equal to number of character in a text file


int find_count_of_chars(const char *str){
    return strlen(str);
}
/*while reading of file we can get how much we read so no need to use this function*/

Code for Finding lines, words and characters in a file using above functions


int txt_my_wc(FILE *fp, int *lines_p, int *words_p, int *chars_p){
        int  lines=0, words=0, chars=0;
        char buffer[1024]={0x00};
        size_t bytes=0;
        fseek(fp, 0L, SEEK_SET);
        do{
                memset(buffer, 0x00, sizeof(buffer));
                bytes = fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
                if(bytes > 0){
                        lines += find_count_of_lines(buffer);
                        words += find_count_of_words(buffer);
                        chars += bytes;
                }
        }while(bytes > 0);
        *lines_p = lines;
        *words_p = words;
        *chars_p = chars;
        return chars;
}

Full Source Code of Program


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


/*Finding Number of new line characters in a buffer*/
int find_count_of_lines(const char *str){
        int liness=0, i=0;
        for(i=0; str[i] != '\0'; i++){
                if( str[i] == '\n' ){
                        liness+=1;
                }
        }
        return liness;
}

int find_count_of_words(const char *str){
        int words=0, i=0;
        // skip initial white space characters
        while(str[i] != '\0'){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                        i++;
                }else{
                        break;
                }
        }
        for(; str[i] != '\0'; i++){
                if( str[i] == ' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t' ){
                        words+=1;
                        // skip consucative white space
                        while(str[i]==' ' || str[i] == '\n' || str[i]== '\r' || str[i] == '\t'){
                                i++;
                        }
                }
        }
        return words;
}

int txt_my_wc(FILE *fp, int *lines_p, int *words_p, int *chars_p){
        int  lines=0, words=0, chars=0;
        char buffer[1024]={0x00};
        size_t bytes=0;
        fseek(fp, 0L, SEEK_SET);
        do{
                memset(buffer, 0x00, sizeof(buffer));
                bytes = fread(buffer, sizeof(char), sizeof(buffer)-1, fp);
                if(bytes > 0){
                        lines += find_count_of_lines(buffer);
                        words += find_count_of_words(buffer);
                        chars += bytes;
                }
        }while(bytes > 0);
        *lines_p = lines;
        *words_p = words;
        *chars_p = chars;
        return chars;
}


int main(int argc, char *argv[]){
        FILE *fp = NULL;
        int lines=0, words=0, chars=0;
        if(argc != 2){
                fprintf(stderr, "USAGE: %s <filename> \n", argv[0]);
                return -1;
        }
        fp = fopen(argv[1], "r");
        txt_my_wc(fp, &lines, &words, &chars);
        printf("  %d  %d  %d  %s\n", lines, words, chars, argv[1]);
        fclose(fp);
        return 0;
}

Compilation and Running


rajesh@ideapad:~/Rajesh/Blog/file$ gcc mywc.c -o mywc
rajesh@ideapad:~/Rajesh/Blog/file$ ./mywc 
USAGE: ./mywc 
rajesh@ideapad:~/Rajesh/Blog/file$ ./mywc mywc.c 
  72  224  2141  mywc.c
rajesh@ideapad:~/Rajesh/Blog/file$ wc mywc.c 
  72  224 2141 mywc.c

1 comment: