C code to count the number of words in a string

February 13, 2010 in Uncategorized

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

21 responses to C code to count the number of words in a string

  1. Check for a

    pointer passed as an argument at the beginning of the function.

    VA:F [1.9.22_1171]
    0
    • 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
  2. #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
  3. 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
    • Idea is good.

      VN:F [1.9.22_1171]
      0
    • 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
  4. 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
    • 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
    • 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
  5. 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
  6. In Java

    VN:F [1.9.22_1171]
    0
  7. VN:F [1.9.22_1171]
    0
  8. 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
  9. void wordCount(char *str, int *count){
    if(!*str){
    return;
    }
    if(*str == ' ') (*count)++;
    wordCount(str + 1, count);
    }

    VA:F [1.9.22_1171]
    0
  10. I have encounter this in my interview

    VN:F [1.9.22_1171]
    0
  11. VN:F [1.9.22_1171]
    0
  12. 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
    • ” a ab,g,,,tt, cde d “

      use space as only possible split seems wrong in real case

      VA:F [1.9.22_1171]
      0
    • 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

Leave a reply

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

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