Thursday, January 27, 2011

C word count program

/*
Word Count Program (delimiter is the space)
Author: Anwar Mamat
Date: 1/27/2011

*/

#define LENGTH 200

#include "stdio.h"

int main()
{
char words[LENGTH] = {"The test of a first-rate intelligence is the ability to hold two opposed ideas in mind at the same time and still retain the ability to function."};

char *p = words;
int wordcount = 0;

while(*p){
//skip spaces
while(*p++ == ' ') {
//if a space is the last, terminate the loop
if(!*p) goto done;
}
//word found
wordcount++;
//skip the word
while(*p != ' ' && *p) p++;
}
done:
printf("%s\n", words);
printf("Number of words = %d\n", wordcount);
}

No comments: