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.6/5 (16 votes cast)
C code to count the number of words in a string, 4.6 out of 5 based on 16 ratings

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

    1. Profile photo of Gourab MitraGourab 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. Profile photo of Gourab MitraGourab 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. Profile photo of DavyDavy

    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. Profile photo of vamsivamsi

    In Java

    VN:F [1.9.22_1171]
    0
  6. Profile photo of xuntxunt

    VN:F [1.9.22_1171]
    0
  7. Profile photo of Ankit SinghalAnkit 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. Profile photo of yaz720yaz720

    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. Profile photo of Yunsong WuYunsong 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. Profile photo of madnomadno

    VN: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.