C code to count the number of words in a string

Count the number of words in a string, where a word is defined to be a contiguous sequence of non-space characters.

eg, “Hello, my name is John.” -> 5

Hint: Take account for extra spaces at the start and end of the string, and there might be more than multiple spaces between words.

Below is a possible solution in C. The key is to note when it is in a word and when it is not; When it changes from not-in-word to in-word, increment wordCount by one.

VN:F [1.9.22_1171]
Rating: 4.7/5 (21 votes cast)
C code to count the number of words in a string, 4.7 out of 5 based on 21 ratings

24 thoughts on “C code to count the number of words in a string

    1. Gourab Mitra

      Its already takes care of by the

      condition. Since wordCount is initialized to zero in the beginning, the loop doesn’t execute even once when a null pointer is passed and the output is 0 which is correct

      VN:F [1.9.22_1171]
      -3
  1. Raj

    #include
    #include
    int wc(char *str)
    {
    int count=0;
    char *tmp=str;

    while(*tmp==’ ‘)
    tmp++; //skip all the white space at beging

    while(*tmp!=”)
    {

    if((*tmp!=’ ‘)&&(*(tmp+1)==’ ‘) )
    count++;

    tmp++;
    }
    tmp–;
    if(*tmp!=’ ‘)
    count++;

    return count;
    }

    int main()
    {
    int t;
    char str[100];
    while(1)
    {
    gets(str);
    printf(“%d\n”,wc(str));
    str[100]=”;
    }

    return 0;
    }

    VA:F [1.9.22_1171]
    +1
  2. Gourab Mitra

    I think the criteria for deciding if the str pointer has come out of a word should be made more robust, something like

    What do you think?

    VN:F [1.9.22_1171]
    +1
    1. Alberto

      the problem statement defines words as a ‘contiguous sequence of non-space characters’, which means ‘,’ and ‘.’ are considered a part of the word. so ‘exp1,exp2’ would be considered one word.

      VA:F [1.9.22_1171]
      +1
  3. GAURAV GUPTA

    void strMyLen ( const char * ptr, int * counter)
    {
    if(*ptr == NULL)
    return;
    (*counter)++;
    strMyLen( ptr +1, counter);
    }

    int main() {
    int counter =0;
    strMyLen (“GAURAV”, &counter);
    printf(“\n The String Count is “, counter );
    return 0;
    }

    VA:F [1.9.22_1171]
    -1
    1. GAURAV GUPTA

      void strMyLen ( const char * ptr, int * counter)
      {
      if(*ptr == NULL)
      return;
      if((*ptr != ‘ ‘) || (*ptr != ‘\t’))
      (*counter)++;
      strMyLen( ptr +1, counter);
      }

      int main() {
      int counter =0;
      strMyLen (“GAURAV”, &counter);
      printf(“\n The String Count is “, counter );
      return 0;
      }

      VA:F [1.9.22_1171]
      -1
    2. Nathan

      public bleooan modThree(int[] nums) { bleooan result = false; for (int i = 0; i < nums.length 2; i++) { int mod = nums[i] % 2; if (nums[i+1] % 2 == mod && nums[i+2] % 2 == mod) { result = true; } } return result;}

      VA:F [1.9.22_1171]
      0
  4. Davy

    int wordCount(char* str)
    {
    assert(str);
    int count=0;
    char* p=str;
    while (*p!=”)
    {
    if ((*p==’ ‘) || (*p==’\t’))
    {
    while ((*p!=”) && ((*p==’ ‘) || (*p==’\t’)))
    {
    p++;
    }
    }
    else
    {
    count++;
    while ((*p!=”) && (*p!=’ ‘) && (*p!=’\t’))
    {
    p++;
    }
    }
    }
    return count;
    }

    VN:F [1.9.22_1171]
    0
  5. vamsi

    In Java

    VN:F [1.9.22_1171]
    0
  6. xunt

    VN:F [1.9.22_1171]
    +1
  7. Ankit Singhal

    int countNumWords(const char *str) {
    int wordCount = 0;
    while (str) {
    if (*str == ‘ ‘ || *str == ‘\n’ || *str == ‘\t’)
    str++;
    else {
    do {
    str++;
    } while ( *str != ” || *str != ‘\n’ || *str != ‘\t’);
    wordCount++;
    }
    }
    return wordCount;
    }

    VN:F [1.9.22_1171]
    0
  8. Azar

    void wordCount(char *str, int *count){
    if(!*str){
    return;
    }
    if(*str == ' ') (*count)++;
    wordCount(str + 1, count);
    }

    VA:F [1.9.22_1171]
    0
  9. yaz720

    VN:F [1.9.22_1171]
    0
  10. c# dev

    I have two questions,
    1. why we need to use inword, could we implement without it
    2. how about below case?

    ” a ab,g,,,tt, cde d “, expected count = 6

    below is my simple c# code

    VA:F [1.9.22_1171]
    0
    1. Yunsong Wu

      In the problem statement, “a word is defined to be a contiguous sequence of non-space characters.” Thus, “ab,g,,,tt,” is one word.

      If you use the correct English grammar, this definition is correct in the real case.

      VN:F [1.9.22_1171]
      0
  11. madno

    VN:F [1.9.22_1171]
    0
  12. skysbjdy

    int count(char *str) {
    int res = 0;
    char* p1 = str;
    while (*p1++ != '\0') {
    if(*p1 != ' ') res++;
    while(*p1 != ' ' && *p1 != '\0') p1++; //pass the current word
    }
    return res;
    }

    VA:F [1.9.22_1171]
    0
  13. skysbjdy

    VA:F [1.9.22_1171]
    0

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use the <code> tag to embed your code.

*