Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Count words in a document

Hello all,


I want to count words in a document. First I have made a program that count letters in a document:



#import <foundation/foundation.h>


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

{


int count={0},n={0},SearchChar;

FILE *document;

document=fopen("/Users/mymac/Desktop/test.txt","r");

SearchChar='H';

while(n!=EOF)

{

if ((n=fgetc(document))==SearchChar) {

count+=1;

}


}

printf("%d\n",count);


fclose(document);

return 0;


}



I have no idea how to count words in a document (as you can see it is a .txt document). Which command do I need fgetc can't handle words, but is there a command that can handle words.


RL6001

Posted on Nov 11, 2012 3:09 AM

16 replies

Count words in a document

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.