user7186159
user7186159

Reputation:

separate numeric char from user input and print

So I'm trying to read user input and print the numerical chars from the input, line by line. This is what I've got so far:

printf("Type characters in a line, with no spaces: \n\n");

scanf(" %c", &inp);

int check(char num, char oth, char inp, char c, int inum)
{
    if(c==0,1,2,3,4,5,6,7,8,9)
    {
        printf("%c", &c);
    }
}

I'm a complete beginner and honestly am completely lost, I would appreciate some help or just for someone to point me in the right direction. Also I can't use strings.

Upvotes: 1

Views: 40

Answers (2)

M.M
M.M

Reputation: 2304

C language has a function, isdigit (defined in ctype.h), that does this verification for you:

#include <ctype.h>
...
int check(char num, char oth, char inp, char c, int inum){
    if(isdigit((unsigned char)c)){
        printf("%c", &c);
    }
}

Upvotes: 1

ForceBru
ForceBru

Reputation: 44858

In most encodings the characters that represent the digits are placed in ascending order starting with '0', so you could simply check whether c is in range ['0', '1', ... '8', '9'] including both the beginning and the end.

int check(char num, char oth, char inp, char c, int inum)
{
    if ((c >= '0') && (c <= '9'))
        printf("%c", c);
}

If you'd like to print the chars line by line, you should print a newline character ('\n') as well: printf("%c\n", c);.

Upvotes: 1

Related Questions