3 Replies - 13043 Views - Last Post: 14 April 2008 - 11:14 PM Rate Topic: -----

#1 steverh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 08

Word Count program in C

Post icon  Posted 14 April 2008 - 07:47 AM

Hello, I need to count the words from a text file but seem to be stuck, my code will compile but either continuous loops or only returns a value of 2. Please assist.


#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <iostream.h>


int main(void)
{
char x;
FILE *wordcount;
int count = 0;

	wordcount = fopen("gettys.txt", "r+");
	
	if (wordcount == NULL)
		printf("\nFile gettys.txt cannot be opened.\n");
	
	while ( !feof(wordcount) )
	{
		fscanf(wordcount, "%c", &x);
		for (count = 0; isspace(x); count++)
			{
			count = count +1;
			}
	}	
	return count;	
	printf("\nThe Gettysburg Address contains %d words.\n", &count);
	fclose(wordcount);
	
}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Word Count program in C

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Word Count program in C

Posted 14 April 2008 - 08:39 AM

well, your for loop should not be incrementing your count variable, use an if statement to check whether isspace(x) is true (returns a non-zero value if some sort of space value is encountered.) Also, do not use return count;, as this will exit your function and return the value of count. Just print off the value like you have and then return 0; or whatever you are used to.
Was This Post Helpful? 0
  • +
  • -

#3 steverh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 31-March 08

Re: Word Count program in C

Posted 14 April 2008 - 10:54 PM

I still am having the same problem of the continuous loop with these changes, are there any further suggestions?

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <iostream.h>


int main(void)
{
char x;
FILE *wordcount;
int count;

	wordcount = fopen("gettys.txt", "r+");
	
	if (wordcount == NULL)
		printf("\nFile gettys.txt cannot be opened.\n");
	
	while ( !feof(wordcount) )
	{
		fscanf(wordcount, "%c", &x);
		for (count = 0; isspace(x); count++)
			{
			if (isspace(x) != '\0') 
			count = count +1;
			}
	}	
		
	printf("\nThe Gettysburg Address contains %d words.\n", &count);
	fclose(wordcount);
		
}



Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5355
  • View blog
  • Posts: 27,314
  • Joined: 10-May 07

Re: Word Count program in C

Posted 14 April 2008 - 11:14 PM

Your problem is that your for loop contains your counter, & then inside of the loop you are using the same variable to count the number of words. So what is happening is you set the rules for the for loop to loop X amount of time, then you change the value of X every time you find a space.

#include <stdio.h>
#include <ctype.h>


int main(void) {
  char x;
  FILE *wordcount;
  int words=0;

  wordcount = fopen("gettys.txt", "r+");

  if (!wordcount) {
    printf("\nFile gettys.txt cannot be opened.\n");
  }

  while (!feof(wordcount)) {
    fscanf(wordcount, "%c", &x);
    if (isspace(x)) {
      words++;
    }
  }

  fclose(wordcount);
  printf("\nThe Gettysburg Address contains %i words.\n", words);

}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1