42-exam-miner
42-exam-miner copied to clipboard
last_word.c bug
Before: $>./last_word "abcd" | cat -e $>bcd$
I correct this problem, by adding
while (av[1][i] && blank(av[1][i]))
i++;
last_word.c
#include <unistd.h>
int blank(char c)
{
if (c == ' ' || c == '\t')
return(1);
return(0);
}
int main(int ac, char **av)
{
unsigned int i;
i = 0;
if (ac == 2)
{
while (av[1][i])
i++;
i--;
while (i > 0 && blank(av[1][i]))
i--;
while (i > 0 && !blank(av[1][i]))
i--;
while (av[1][i] && blank(av[1][i]))
i++;
while (av[1][i] && !blank(av[1][i]))
{
write(1, &av[1][i], 1);
i++;
}
}
write(1, "\n", 1);
return(0);
}