954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problems in Word Count

Im a beginner in C,Can anyone help me? :?:

I have problems when I encounter double or multiple spaces between words.

#include <stdio.h>
FILE *fpr;
int main(int argc,char *argv[])

{
	if ((fpr = fopen(argv[1],"r")) == 0)
	{
		printf ("\nFile %s Don't Exist!\n", argv[1]);
	}
	fprintf (fpr,"%d",count());
	return(0);
}

count()
{
	char ch;
	int word=0;
		while(feof(fpr) == 0)
		{
			ch= getc(fpr);
			if(ch == ' ')
			{
				word++;
			}
			else if(ch == '\n')
			{
				word++;
			}
		}
		printf("Number of Words : %d\n",word);
}


Thank You :)

dizzyboy
Newbie Poster
3 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Instead of the simple if(char == space) logic, (which obviously can't handle multiple spaces), how about this.

inword = 0; changes to inword=1 when you find a word. When you leave a word, it goes back to 0.

First letter you run across, you set it to 1, and increment word counter. When you run into the first space, you set it to 0, and do not increment word count any more, until you run into the next letter.

You need to add some logic to handle things like the last word in a sentence, at the end of a file, etc.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Thank you Adak for giving me ideas.

inword = 0; changes to inword=1 when you find a word. When you leave a word, it goes back to 0.

am I on the right path?

if(!inword)
				{
					inword=1;
					word++;
				}
				else if(inword)
				{
					inword=0;
				}


thanks again

dizzyboy
Newbie Poster
3 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

Not yet - that's the same logic as before, using different variables. :(

in the header file ctype.h, we have functions to test what kind of char it is. I'd use two of them, so include at the top of your program.

Those two are ispunct() (is it a punctuation, and isalpha(), is it a regular alphanumberic char (letter or number)?

So the logic will be (approximately):

if((inword  equals 0) and (isalpha(char being checked))) { //
  inword = 1
  wordcount increments by one
}
/* handles last word in a row which has no space after it. */
else if(char equals ' ' or !isalpha(char being checked)) {
  inword = 0
}

May be more logic needed, but not much. It's been a long time since I coded one up. Give this a try, and post back up and I'll study it more.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

Edit: this seemed to work OK, in my very limited testing.

fp is the file pointer, ch is a char, and the other variables are int's. ctype.h was included, of course.

while((ch=getc(fp)) != EOF) {
    putchar(ch);
    if((isalpha(ch)) && (inword==0)) {
      inword=1;
      wordcount++;
    }
    else if(!isalpha(ch)) { //isalpha(ch)==0
      inword=0;
    }
  }
  fclose(fp);
  printf("\n I counted %d words in that file\n", wordcount);

Give this a try, and if no luck, post back, but I think you'll get it from this.

Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
 

By the way, isalpha() only checks for Letters, isalnum() checks for Letters and Digits..

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

Edit: this seemed to work OK, in my very limited testing.

fp is the file pointer, ch is a char, and the other variables are int's. ctype.h was included, of course.

while((ch=getc(fp)) != EOF) {
    putchar(ch);
    if((isalpha(ch)) && (inword==0)) {
      inword=1;
      wordcount++;
    }
    else if(!isalpha(ch)) { //isalpha(ch)==0
      inword=0;
    }
  }
  fclose(fp);
  printf("\n I counted %d words in that file\n", wordcount);

Give this a try, and if no luck, post back, but I think you'll get it from this.

It worked!! Thank you very much!! :)

@WaltP: Thanks also for the info! :cool:

dizzyboy
Newbie Poster
3 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: