42-exam-miner icon indicating copy to clipboard operation
42-exam-miner copied to clipboard

last_word.c bug

Open Ericfreespirit opened this issue 4 years ago • 0 comments

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);
}

Ericfreespirit avatar May 27 '20 14:05 Ericfreespirit