top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Counting number of words (only count words which have at least one alphabetic character) in a File using C?

+2 votes
332 views

Words may be separated by any amount of whitespace characters. There can be integers in a file, but the program should only count words which have at least one alphabetic character.

posted Apr 21, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

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

int FindWord(char *);

main(int argc,char **argv)
{
    File *fp;
    char str[100];
    int count=0;

    fp = fopen(argv[1],"r");
    if(fp==NULL)
    {
        printf("Unable to open a file\n");
        return;
    }

    while(fgets(str,sizeof(str),fp)!=NULL)
    {
        count=count+FindWord(str);
        bzero(str,sizeof(str));
    }
    printf("Total Word are %d\n",count);
}

int FindWord(char *p)
{
    int count=1;
    int i,j;
    for(i=0;p[i];i++)
    {
        if(p[i]==' ')
        {
            for(j=i+1;p[j];j++)
            {
                if(p[j]!=' ')
                    break;
            }
            count+=1;
            i=j;
        }
    }

    return count;
}
answer Apr 22, 2015 by Chirag Gangdev
This may not be covering all the cases.
1. If line has spaces in the end it may count the wrong numbers
2. If a word has only numbers say 1111 then it should not be counted which is not considered.

Do you like to correct the program :)
Similar Questions
+4 votes

Say you are given a 2X2 board then number of squares are 5. Can someone help with the logic and sample program?

+3 votes
#include<stdio.h>
#include<string.h>

int main()
{
    char ptr[]= {'a','b','c','0','e'};
    char str[]= "abc0e";
    printf("\nptr = %s\t len = %d\n",ptr, strlen(ptr));
    printf("\nstr = %s\t len = %d\n",str, strlen(str));
    return 0;
}

Output : ptr = abc0e len = 6
str = abc0e len = 5

Why the length for ptr is 6 ? Can someone please explain it ?

...