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 :)

Recommended Answers

All 6 Replies

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.

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

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 <ctype.h> 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.

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.

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

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:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.