-1

I have I problem. I get 2 warnings from console, but I dont know what's wrong with my code. Can you have look? Program suppose to show lines with at least 11 characters and 4 numbers

#include <stdio.h>
#include <ctype.h>


int main()
{
    char line[200];

    printf("Enter a string: \n");
    while(fgets(line, sizeof(line),stdin))
    {
        int numberAlpha = 0;
        int numberDigit = 0;
        if(isalpha(line)) numberAlpha++;
        else if(isdigit(line)) numberDigit++;

        if(numberAlpha+numberDigit>10 && numberDigit>3) printf("%s \n", line);
    }
    return 0;
}
too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Struziu
  • 73
  • 4

4 Answers4

1

isalpha() and isdigit() functions take an int. But you are passing a char* i.e. the array line gets converted into a pointer to its first element (see: What is array decaying?). That's what the compiler complains about. You need to loop over line to find the number of digits and alphabets in it.

Also note that fgets() will read in the newline character if line has space. So, you need to trim it out before counting.

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

int main(void)
{
    char line[200];

    printf("Enter a string: \n");
    while(fgets(line, sizeof(line),stdin))
    {
        int numberAlpha = 0;
        int numberDigit = 0;

        line[strcspn(line, "\n")] = 0; // Remove the trailing newline, if any.

        for (size_t i = 0; line[i]; i++) {
        if(isalpha((unsigned char)line[i])) numberAlpha++;
        else if((unsigned char)isdigit(line[i])) numberDigit++;
        }

        printf("alpha: %d, digits:%d \n", numberAlpha, numberDigit);
    }
    return 0;
}
Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238
1

Both isalpha() and isdigit() takes an int, not a char *, as argument.

In your code, by passing the array name as the argument, you're essentially passing a char * (array name decays to the pointer to the first element when used as function argument), so, you're getting the warning.

You need to loop over the individual elements of line and pass them to the functions.

That said, just a suggestion, for hosted environment, int main() should be int main(void) to conform to the standard.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

isalpha and isdigit are supposed to test if a char taken as int (a char can be safely converted to an int) is the encoding of an alphanumeric or digit character. You pass a char array, not an individual char. You need to test each char of the string you got, so you need a loop as:

for (int i=0; i<strlen(line); i++) {
    if (isalpha(line[i])) numberAlpha++;
    ...
}

It is better to compute the length once:

int length = strlen(line);
for (int i=0; i<length; i++) {
    ...
}

You may also use a pointer to move along the string:

for (char *ptr = line; *ptr!=`\0`; ptr++) {
    if (isalpha(*ptr)) ...
    ...
}
Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
1

Ok, i got something like this:

#include <stdio.h>
#include <ctype.h>


int main()
{
    char line[200];

    printf("Enter a string: \n");
    while(fgets(line, sizeof(line),stdin))
    {
        int numberAlpha = 0;
        int numberDigit = 0;
        int i;
        for(i=0; i<strlen(line); i++){
            if(isalpha(line[i])) numberAlpha++;
            else if(isdigit(line[i])) numberDigit++;
        }

        if(numberAlpha+numberDigit>10 && numberDigit>3) printf("%s \n", line);
    }

    return 0;
}

Now the question is, if it is passible to make it first accepts data and then display only those line which follows the if statment. Now it shows line just after input it.

Struziu
  • 73
  • 4