I tried to check if a character of a string is alnum and then if the string contains only alnum characters to print the string. When I run the program nothing happens. I have another program with whom I read from the input the text I want and I send it with FIFO. If i don;t include in the program the "check" function it works, after that it doesn't.
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 don't put it together 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 use to check if a string contains just alnum characters. But if I don;t include this function in my program, the program works. I think here is the problem. 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);
}
The Input text contains '\n' and that means that the string is not an alphanumeric string. When I read I push the enter button and that means one more character in the string.