0

sorry if this question has already been asked here, I didn't find answer, so I'm trying to do a programme for count somme lettre in text, here is my code:

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

// ƒ to count the lettre E
void lettreCount(char *saisi, int length){
    int i, count = 0;

    for (i = 0; i  < length; i++){  //loop for find and count lettre
    
        if (strchr(&saisi[i],"e")){  //verification of lettre
            ++i;
            count++;
        }
    } 

    printf("Dans votre text il y a %d de lettre \"e\"\n", count);
}


int main(){
    char text[132]; //Array for text
    int len;

    printf("Saisissez le text:\n");
    gets(text);

    len = strlen(text);

    lettreCount(text, len);
    
}

but i get all time this warning:

incompatible pointer to integer conversion passing 'char [2]' to parameter of type 'int' [-Wint-conversion]
                if (strchr(&saisi[i],"e")){  //verification of lettre
                                     ^~~

what do i do wrong?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    `"e"` should be `'e'`. It gets the character itself, not a string. – mch Jan 08 '22 at 14:10
  • Just to clarify a bit, the `"e"` argument will pass to `strchr` a _pointer_ (value of type `char*`) to the memory location where the `"e"` string is stored, while the `'e'` will pass the character itself (value of type `char`). – tromgy Jan 08 '22 at 14:18

1 Answers1

0

The function strchr expects as its second argument an object of the type int.

char *strchr(const char *s, int c);

However you supplied a string literal that is implicitly converted to the type char *.

if (strchr(&saisi[i],"e"))

You should write at least like

strchr(&saisi[i], 'e' )

Also pay attention to that the function expects that the first argument contains a string.

So this function declaration

void lettreCount(char *saisi, int length){

within which there is used the standard string function strchr does not make a sense.

Also this for loop

for (i = 0; i  < length; i++){  //loop for find and count lettre

    if (strchr(&saisi[i],"e")){  //verification of lettre
        ++i;
        count++;
    }
} 

also does not make a sense and has bugs. For example if to update the call of strchr the same character 'e' can be counted several times or some letters in the string can be skipped from counting.

The function can be declared and defined the following way

size_t lettreCount( const char *s, int c )
{
    size_t n = 0;

    for ( ; ( s = strchr( s, c ) ) != NULL; ++s )
    {
        ++n;
    }
   
    return n;
}

And in main the function can be called like

printf( "Dans votre text il y a %zu de lettre \"e\"\n", lettreCount( text, 'e' ) );

If to declare the function with the parameter that specifies the size of the character array then the function can be declared and defined the following way

size_t lettreCount( const char *s, size_t n, int c )
{
    size_t count = 0;

    for ( size_t i = 0; i < n; i++ )
    {
        if ( s[i] == c ) ++count;
    }
   
    return count;
}

Also bear in mind the that function gets is unsafe and is not supported by the C Standard. Instead use the function fgets.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335