vendredi 27 février 2015

Counting words in a Word Count Program from a filename ( Linux) (C)



I seem to be able to get the lines, characteres, and spaces down for my output on my program. However, Im having a hard time finding out what to do in order to count words, they dont have to be in dictionary form. For example, fdasf fdjsak fea would be three words. This is what I have :



#include <stdio.h>




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

int ccount=0;
int wcount=1;
int lcount=0;
int scount=0;

char* filename = argv[1];
printf( "filename is: %s\n", filename );

FILE* infile;
infile = fopen( filename, "r" );

if( infile == NULL )
{
printf( "%s: is not a valid file\n", filename );
return -1;
}

char c;
while( (c = fgetc(infile)) != EOF )
{
if(c== ' '){
wcount++;
}

if( c == '\n' )
{
lcount++;
}

if( c != ' ' || c!= '\n'){
ccount++;
}
if(c == ' ' ){
scount ++;
}



}
printf( "total number of lines: %d\n",lcount );
printf( "total number of characters: %d \n" , ccount);
printf( "total number of non-whitespace characters: %d \n", scount );
printf( "total number of words: %d \n" , wcount);

return 0;


}




Aucun commentaire:

Enregistrer un commentaire