by Dinesh Thakur Category: Puppetting On Strings

We may want to know the number of words in a given string and the average word length. The English text contains words (i. e., sequences of letters) separated by spaces, newlines and punctuation marks such as period, comma, semicolon, colon, quotation marks, exclamation, question mark, etc. It is quite a difficult job to separate the words from these punctuation marks. However, this task is greatly simplified when we use the powerful strtok function provided in the standard C library. The program given below determines the number of words in a given string and the average word length.

The main function initializes character array str with a string literal. A character array punct_str is used as a string of punctuation characters and is initialized with the string literal " .,;:!?'\"".This string is used by strtok to separate the words in string str. Since the strtok function modifies the string being processed, we create a duplicate of this string usingthe strdup function and operate on this string. A character pointer tmp_str is used to point to this string.

The main function first displays string str. Initially, the counters nword and nchar are Initialized to 0 and the given string is duplicated and tmp_str is made to point to it. The strtok function is used to separate first word from string tmp_str. The character pointer wptr is used to point to this word. Then a while loop is set up to process the remaining words in string tmp_str.

/* determine number of words and average word length in a given string */

#include <stdio.h>

#include <string.h>

int main()


     char str[] = "Alexander said, \"I came, I saw, I conquered!\"";

     char punct_str[] =" .,;:!?'\""; /* punctuator list*/

     char *tmp_str; /* copy of given string */

     char *wptr; /* pointer to a word */

     int nword, nchar; /* no of words and no of chars in all words */

     float avg_len; /* average word length */


     printf("Given string: %s\n", str);

     puts("\nWords in a given string:");

     nword = nchar = 0;

     tmp_str = strdup(str);       /* copy of given string */

     wptr = strtok(tmp_str, punct_str); /* get ptr to first word */

     while (wptr != NULL)


                  nword++;      /* increment word count */

                  nchar += strlen(wptr);    /* update character count */

                  printf("%s ", wptr);    /* display current word */

                  wptr = strtok(NULL, punct_str);  /* get ptr to next word */


                 avg_len = (float) nchar / nword; /* calc average word length */

                 printf("\n\nTotal words: %d\n", nword);

                 printf("Average word length: %4.lf\n", avg_len);


                 return 0;


In each iteration of the loop, first the nword and nchar counters are updated. Then the current word pointed to by wptr is displayed and the strtok function is called to separate the next word from string tmp_str. The program output is given below.

                  C program to Average Character Per Word in a String

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.