Total Pageviews

PRASO's Solution for EMBETRONIX & ROBOTIX

SOLUTION PROVIDER OF ANY SORT OF PROBLEMS RELATED TO ELECTRONICS AND TELECOMMUNICATION

Monday, February 28, 2011

./wc : : word count , determining the size of a file (in bytes, kilo bytes, in mega bytes) , no of characters in a file , no. of Lines in a File , No. of Words in a File , and arrangment in a descent order in the target file..... USING C in LINUX and UNIX

#include "stdio.h"
#include "string.h"
#include "malloc.h"
main(int argc, char **argv)
{
FILE *fp,*ft;
int count=0, i=0 , size=0 , word_count=0;
char *str,ch,c;
char *ptr;

    if(argc!=3)
    {
        printf("ERROR: Invalid no. of Arguments\n");
        printf("Usage: wc \n");
        return;
    }

fp=fopen(argv[1],"r");

/* Determining the Size of the File */

fseek(fp,SEEK_SET,SEEK_END);
size=ftell(fp);
printf("The size of the file is : %d bytes or %d KiloBytes or %d Megabytes\n",size,size/1024,size/(1024*1024));
fclose(fp);


size++;

str = calloc(1,size);  // Allocate the size required to storethe Characters of the FILE into a String
fp=fopen(argv[1],"r");

/* Storing the Characters of the File into a Character Array or String */

for(i=0;i
    str[i]=fgetc(fp);

fclose(fp);

      /* Operation on The String */

printf("The Number of Characters = %d\n", strlen(str));  //The Number of Characters in the file
   
   /* Number of Lines */

    for(i=0;str[i];i++)
    {
        if(str[i]=='\n')
        {
            count++;
        }
    }

printf("No of lines: %d\n",count);

    /* Remove Extra Blank Spaces (If any) and then Calculate the Number of Words */

  /* Remove Blank Spaces */

ptr=str;
    while(ptr=strchr(ptr,' '))
    {
        if(ptr[1]==' ')
        {
            strcpy(ptr+1,ptr+2);
            ptr--;
        }
        ptr++;
    }

   /* Remove Consecutive New Lines */

ptr=str;

    while(ptr=strchr(ptr,'\n'))
    {
        if(ptr[1]=='\n')
        {
            strcpy(ptr+1,ptr+2);
            ptr--;
        }
        ptr++;
    }

/* Removing Space from the starting of a line */

ptr=str;
    while(ptr=strchr(ptr,'\n'))
    {
        if(ptr[1]==' ')
        {
            strcpy(ptr+1,ptr+2);
            ptr--;
        }
         ptr++;
    }

/* Removing consecutive NEW line */

ptr=str;

    while(ptr=strchr(ptr,'\n'))
    {
        if(ptr[1]=='\n')
        {
            strcpy(ptr+1,ptr+2);
            ptr--;
        }
        ptr++;
    }


       /* Count Number of Words */

for(i=0,count=0;str[i];i++)
    {
        if(str[i]=='\n')
        {
            count++;
        }
    }

for(i=0;str[i];i++)
    {
        if(str[i]==' ')
        {
            word_count++;
        }
    }

printf("No of words: %d\n",(word_count+count));

ft=fopen(argv[2],"w");
            fputs(str,ft);

fclose(ft);

}




OUTPUT:



Source File : demon

Target File : filter


------------------------------------------ USAGE OF THE command----------------------------------

somanath@ubuntu:~/programs/files$ cc wordcount.c -o wc

somanath@ubuntu:~/programs/files$ ./wc demon filter
The size of the file is : 329 bytes or 0 KiloBytes or 0 Megabytes
The Number of Characters = 329
No of lines: 18
No of words: 27

-------------------------------------  CONTENTS OF SOURCE FILE  --------------------------------------

somanath@ubuntu:~/programs/files$ ./readfile demon
ravan    is    a demon





                                                                                      SOM          is       a       hero

        SOM killed ravan



    Ravan     got assacinated by     Somanath     Nanda        in a      game of      gambling.......          in splits    VILAA



--------   PRASO



-------------------------------------  CONTENTS OF TARGET FILE  --------------------------------------

somanath@ubuntu:~/programs/files$ ./readfile filter
ravan is a demon
SOM is a hero
SOM killed ravan
Ravan got assacinated by Somanath Nanda in a game of gambling....... in splits VILAA
-------- PRASO

No comments: