Home > Software design >  What is the data type of text[i]?
What is the data type of text[i]?

Time:10-29

I'm trying to count the number of words in a string entered by the user with this custom function:

int count_words(char* text)
{
   int wc = 0;
   for(int i = 0,k = strlen(text); i<k;i  )
   {
      if(strcmp(" ",text[i]) ==0 || strcmp(".",text[i]) ==0 || strcmp("!",text[i]) ==0 || strcmp("?",text[i]) ==0 || strcmp(",",text[i]) ==0)
      {
          wc  ;
      }
   }
    return wc 1;
}

but I keep getting this error:

re.c:59:21: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion]
      if(strcmp(" ",text[i]) ==0 || strcmp(".",text[i]) ==0 || strcmp("!",text[i]) ==0 || strcmp("?",text[i]) ==0 || strcmp(",",text[i]) ==0)
                    ^~~~~~~
                    &

re.c:59:48: error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & [-Werror,-Wint-conversion]
      if(strcmp(" ",text[i]) ==0 || strcmp(".",text[i]) ==0 || strcmp("!",text[i]) ==0 || strcmp("?",text[i]) ==0 || strcmp(",",text[i]) ==0)
                                               ^~~~~~~
                                               &

What is really happening?

CodePudding user response:

strcmp takes char * (string) not a single char. It's better if you use strtok` to count the number of words in a string.

Here's a simple example

#include <stdio.h>
#include <string.h>

int main(void) {

  char arr[] = "Temp hello bye! No way. Yes";
  char *delimeters = " !.";

  char *token = strtok(arr, delimeters);
  int count = 0;

  while (token != NULL) {
    count  ;
    token = strtok(NULL, delimeters);
  }
  printf("Words = %d", count);

  return 0;
}

You can use strpbrk instead of strtok as well.

CodePudding user response:

strcmp takes an array of char which is end with NULL '\0'. For your question, you need to increment number of words when you find your special characters. and you should check there is no repeated special char respectively.

/*const special_char[] = {' ','.','!','?',','};*/

int count_words(char* text)
{
    uint32_t idx = 0;
    int wc = 0;

    /*if the first char equal one of special char, don't count that as word*/
    for (int i = 1; i < strlen(text); i  )
    {
        /*Special char shouldn't be repeated respectively otherwise, don't count that as word*/
        if (
            ((' ' == text[i]) || ('.' == text[i]) || ('!' == text[i]) || ('?' == text[i]) || (',' == text[i]))
            && 
            ((' ' != text[i - 1]) && ('.' != text[i - 1]) && ('!' != text[i - 1]) && ('?' != text[i - 1]) && (',' != text[i - 1]))
        )
        {
            wc  ;
        }
    }

    /*For the last word*/
    wc  ;

    return wc;
}

CodePudding user response:

The function strcmp is declared the following way

int strcmp(const char *s1, const char *s2);

As you can see the both parameters of the function have the pointer type const char *.

But in this call of the function used by you

strcmp(" ",text[i])

the second argument has the type char. So the compiler issues the error message.

error: incompatible integer to pointer conversion passing 'char' to parameter of type 'const char *';

Also your function has logical bugs. For example for an empty string it returns the number of words equal to 1.

return wc 1;

And each separator it counts as a word. So for a string that contains only separators the function interprets it as containing several words.

Apart from this the function count_words shall not change the passed string. It should be declared like

size_t count_words( const char *text );

The function can be defined the following way as it is shown in the demonstration program below.

#include <stdio.h>
#include <string.h>

size_t count_words( const char *text )
{
    const char *separators = " \t.,!?";
    size_t n = 0;
    
    while ( *text )
    {
        text  = strspn( text, separators );
        
        if ( *text )
        {
              n;
            text  = strcspn( text, separators );
        }
    }
    
    return n;
}


int main(void) 
{
    const char *text = "We, beginners, should help each other!";
    
    printf( "There are %zu words in the text \"%s\".", count_words( text ), text );

    return 0;
}

The program output is

There are 6 words in the text "We, beginners, should help each other!".

A more general approach is when the user of the function can call it specifying by himself required separators of words. For example

#include <stdio.h>
#include <string.h>

size_t count_words( const char *text, const char *separators )
{
    size_t n = 0;
    
    while ( *text )
    {
        text  = strspn( text, separators );
        
        if ( *text )
        {
              n;
            text  = strcspn( text, separators );
        }
    }
    
    return n;
}

int main(void) 
{
    const char *text = "We, beginners, should help each other!";
    
    printf( "There are %zu words in the string \"%s\".", count_words( text, " \t.,!?" ), text );

    return 0;
}

The program output is the same as shown above

There are 6 words in the text "We, beginners, should help each other!".
  • Related