Aatish Sai
Aatish Sai

Reputation: 3

C: counting number of words (need help fixing)

I am new to C programming and trying to write a code for counting the number of words in a string.Here is my code for counting the number of codes.

    #include<stdio.h> 
    #include<string.h>
    void main() 
    { 
        int count=0,i,len; 
        char str[100]; 
        printf("enter the sentence"); 
        gets(str); 
        len=strlen(str); 
        for(i=0;i<=len;i++) 
        {  
           if(str[i]==' ') 
              count++; 
        } 
        printf("the number of words are :\t%d",count+1); 
    }

When my input is:Here is four words it works fine. it gives output the number of words are : 4

My question is how do I handle "two consecutive spaces" between the word, "space at the beginning" of the input and "space at the last" of the input.

Upvotes: 0

Views: 14665

Answers (8)

ZXcvbnM
ZXcvbnM

Reputation: 631

Why not use strtok and bypass it altogether:

int main()
{
    int num_words = 0;
    char    str_one[] = "This string has a trailing space ";
    char    str_two[] = " This string has a preceeding space";
    char    str_three[] = "This string contains  two spaces consecutively  twice!";
    char    delim[] = " ";
    char    *ret;

    /* fgets() for user input as desired... */

    if (( ret = strtok(str_one, delim)) != NULL )
    {
        while ( ret )
        {
            num_words++;
            ret = strtok(NULL, delim);
        }
    }
    else
    {
        /* no spaces, but might contain a word if the string isn't empty */
        if ( str_one[0] != '\0' )
            num_words = 1;
    }

    printf("str_one contains %i words\n", num_words);
    num_words = 0;

    ...

    return 0;
}

And by the way: main should ALWAYS return!!!

Upvotes: 0

printfmyname
printfmyname

Reputation: 971



One quick way to do this is use strtok and break everything according to a predicate. This function satisfy all your requirements.

#include<stdio.h>
#include<string.h>
int countSpace(char* str){
int counter = 0;
char * newString;
newString= strtok (str, " "); // now the newString has the str except first word
while (newString != NULL){
    counter++; // Put counter here to ignore the newString == NULL 
                // Or just -1 from the counter on main()
    newString= strtok (NULL, " "); //Break the str in to words seperated by spaces

}
return counter;
}
void main(){
    int count=0,i,len;
    char str[100];
    printf("Enter the sentence:\n");
    fgets (str , 100 , stdin);
    count = countSpace(str);
    printf("The number of words are :\t%d\n",count);
    return 0;
}

Thank you

Upvotes: 0

Drew
Drew

Reputation: 31

Instead of counting spaces, count the first non-space character of each word.

#include<stdio.h> 

int main() 
{ 
    int count=0; 
    char str[100]; 
    printf("enter the sentence"); 
    gets(str);

    char *cur= str;

    for (;;)
    {
        while (*cur == ' ')
        {
            cur++;
        }

        if (*cur == 0)
        {
            break;
        }

        count++;

        while (*cur != 0 && *cur != ' ')
        {
            cur++;
        }
    }

    printf("the number of words are :\t%d",count); 

    return 0;
}

Upvotes: 3

Anurag Kalia
Anurag Kalia

Reputation: 4798

You should count the transitions from space to non-space characters + a possible non-space character in the beginning itself.

#include<stdio.h> 
#include<string.h>
int main() 
{ 
    int count=0,i,len, cur_is_spc; 
    char str[100]; 
    printf("enter the sentence"); 
    gets(str); 
    len=strlen(str);

    cur_is_spc = 0;           // 0, if current character is not space. 1, if it is.

    for(i=0; str[i]!='\0'; i++) 
    { 
       if(str[i] != ' ')
       {
           switch(cur_is_spc) // currently holding value for previous character
           {
           case 0: count++; break;    //count the spc->non-spc transitions
           case 1:          break;
           default: cout << "Erroneous value"; exit(1);
           }
           cur_is_spc = 1;    //updated for current character.
       }
       else
       {
           cur_is_spc = 0;    //updated for current character.
       }
    } 

    printf("the number of words are :\t%d",count+1); 
    return 0;
}

Here, I am checking with only spaces. But there can be characters like newline, tab etc. How would your code handle them? Hint: use isspace() function.

/moreover, the transition can be done from non-alphabet characters to alphabet characters if you decide that words are made up of alphabets only. This approach is inherently flexible to suit your needs.

Upvotes: 0

ccc
ccc

Reputation: 93

use strtok and first call of strtok use strtok(string," ") and for rest of calls use strtok(NULL, " \n")

Upvotes: 0

johnchen902
johnchen902

Reputation: 9599

Just ignore spaces at the beginning and spaces directly after other spaces, and +1 if there are no spaces at the last.

#include <stdio.h> 
#include <string.h>
// #include <stdlib.h>
int main() // void main is a bad practice
{ 
    int count = 0, i, len, ignoreSpace; 
    char str[100];
    printf("enter the sentence\n"); 
    gets(str); 
    len = strlen(str); 
    ignoreSpace = 1; // handle space at the beginning
    for(i = 0; i < len; i++) // not i<=len
    { 
        if(str[i] == ' '){
            if(!ignoreSpace){
                count++;
                ignoreSpace = 1; // handle two or more consecutive spaces
            }
        }else{
            ignoreSpace = 0;
        }
    }
    if( !ignoreSpace ) // handle space at the last
        count++;
    printf("the number of words are :\t%d\n", count); // +1 is moved to previous line
    // system("pause");
    return 0;
}

Upvotes: 0

Deepu
Deepu

Reputation: 7610

I think the loop in the current form may not work properly,

It should be as follows,

    for(i=0;i<len;i++) 
    { 
          if(i!=0)
          {
             if(str[i]==' ') 
               count++; 
          }
    }  

To check the other criteria change the code as follows,

    for(i=0;i<len;i++) 
    { 
             if(str[i]==' ') 
             {  
                if(i!=0)
                {  
                   if(str[i+1]!=' ')
                   {
                      count++;
                   } 
               }   
    }  

Upvotes: 0

Tianyun Ling
Tianyun Ling

Reputation: 1097

You can use:

 while(str[i]==' '&&str[i]!=EOF) 
{
    count++;
    i++;
}

instead of your if part. You also need to add these code before the for loop to read the beginning spaces.

Upvotes: 0

Related Questions