How to use isalnum function correctly in C?
I tried to check if the string character is alnum and then if the string only contains alnum characters to print the string. When I run the program, nothing happens. I have another program with which I read from the input the text I want and I send it with a FIFO. If I do not include the "check" function in the program, it will work, after which it will not.
void put_alphanum(char *str)
{
while (*str)
{
if (*str >= '0' && *str <= '9')
write(1, str, 1);
else if (*str >= 'A' && *str <= 'Z')
write(1, str, 1);
else if (*str >= 'a' && *str <= 'z')
write(1, str, 1);
str++;
}
write(1,"\n",1);
}
This is the function I used to print the string. If I am not compatible with the check function, the program works.
int check(char *str)
{
while(*str)
{
if(isalnum(*str)==0)
return 0;
str++;
}
return 1;
}
This is the function I am using to check if a string contains only alnum characters. But if I don't include this feature in my program, the program works. I think this is the problem here. And the main function ()
int main()
{
int fd;
int len;
char buf[BUFF_SIZE + 1];
mkfifo(FIFO_LOC, 0666);
fd = open(FIFO_LOC, O_RDONLY);
while(1)
{
while((len = read(fd, buf, BUFF_SIZE)))
{
buf[len] = '\0';
if(check(buf)==1)
put_alphanum(buf);
}
}
return (0);
}
+3
source to share