Monday, 27 May 2013

C implementation of wc(word count) linux command


Type the below program as it is to implement wc command

int main(int argc , char *argv[])
{
 int character_count = 0;
           int space_count = 0;
           int word_count = 0;
           int line_count = 0;
           char ch;

           FILE *fp;

           if(argc == 0)
           {
   printf("Please specify the filename as argument\n");
                      exit(0);
           }

            if(fp != 0)
           {
                       printf("No such file or directory\n");
                       exit(0);
           }
           fp = fopen(argv[1],"r");

           while((ch=fgetc(fp))!=EOF)
           {
                       character_count++;
                       if(ch == ' ')
                       {
                             space_count++;
                             word_count++;
                       }
                       if(ch == '\n')
                       {
line_count++;
                       }
           }
           printf("character_count = %d\n",character_count);
           printf("space_count = %d\n",space_count);
           printf("word_count = %d\n",word_count+1);
           printf("line_count = %d\n",line_count);


}

Follow this to compile and execute the above  program


Step 1 : To compile any C/C++ program, C/C++ compiler like gcc or g++ are required, if you do not have
              one you can download it by just typing below mentioned command in the terminal,

                                                  For C :      sudo  apt-get  install  gcc
                                                 For C++ :   sudo  apt-get  install  g++


Step 2 : Having downloaded C/C++ compiler, compile your program as

                                                  For C :       gcc  myprogram.c
                                                 For C++ :   g++  myprogram.cpp


Step 3 : Now since your program compiled successfully, a file by the name a.out being generated,                        
              which is your executable file. In order to execute this type
         
                                                                            ./a.out
             

As like me, I hope that you too don't like the name of your executable file to be a.out. In order to          
             give your own name for the executable file, type the following command in the terminal

                                              gcc  -o  <the name you wish to give>  <filename>



             ex:- gcc  -o  jamesbond  myprogram.c

             Now your executable file is jamesbond rather than a.out which can be executed as  

                                                                        ./jamesbond                     
             
             Note : use .c extension to store C files and .cpp for C++ files


To make this code as a wc command , follow this



Step 1 : Save the program and compile it to get a.out file
             
Step 2 : Place your executable file (a.out)  in "/usr/bin" path where all the binary files will be present as    
              follows, before that you can change the name of the a.out to any name you like ...

               I have changed to "mycommand" using following command
                                                           
                                                                  gcc -o  mycommand  myprogram.c
                                         

                                                command: sudo cp mycommand  /usr/bin


Step 3 Now execute "mycommand" as you use to do with built in Linux commands.


You can download the source code from here

Leave your doubts or suggestions in comments section..

6 comments:

  1. When compile above code it show 2 errors & lot's of warnings.

    compile on Ubuntu 13.04

    it say FILE & EOF is undeclared...

    ReplyDelete
  2. after compile :
    wct.c
    wct.c: In function ‘main’:
    wct.c:9:12: error: unknown type name ‘FILE’
    wct.c:13:4: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    wct.c:14:23: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    wct.c:19:24: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    wct.c:20:24: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    wct.c:22:15: warning: assignment makes pointer from integer without a cast [enabled by default]
    wct.c:24:34: error: ‘EOF’ undeclared (first use in this function)
    wct.c:24:34: note: each undeclared identifier is reported only once for each function it appears in
    wct.c:37:12: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]

    ReplyDelete
    Replies
    1. use the following header files in the beginning of the program

      #include
      #include
      #include

      Delete
  3. Here is the code with the header files being added

    #include
    #include
    #include
    int main(int argc , char *argv[])
    {
    int character_count = 0;
    int space_count = 0;
    int word_count = 0;
    int line_count = 0;
    char ch;

    FILE *fp;

    if(argc == 0)
    {
    printf("Please specify the filename as argument\n");
    exit(0);
    }

    if(fp != 0)
    {
    printf("No such file or directory\n");
    exit(0);
    }
    fp = fopen(argv[1],"r");

    while((ch=fgetc(fp))!=EOF)
    {
    character_count++;
    if(ch == ' ')
    {
    space_count++;
    word_count++;
    }
    if(ch == '\n')
    {
    line_count++;
    }
    }
    printf("character_count = %d\n",character_count);
    printf("space_count = %d\n",space_count);
    printf("word_count = %d\n",word_count+1);
    printf("line_count = %d\n",line_count);


    }

    ReplyDelete
  4. Thank you for the code
    But word count by the program and WC command are not same.
    I think word count is not incrementing for last word every line

    ReplyDelete
  5. Thanks and that i have a super provide: How Much Are House Renovations Stardew Valley exterior house remodel

    ReplyDelete