Yesterday I had to solve an exam exercise, which, unfortunately, I failed.. The exercise was to create a function in C with the following rules:
- Write a function that takes a string and displays the string in reverse order followed by the newline.
- Its prototype is constructed like this : char *ft_rev_print (char *str)
- It must return its argument
- Only allowed to use function 'write'(so no printf or others)
With that information I wrote :
int ft_strlen(char *str) /*to count the length of the original string*/
{
int i;
i = 0;
while (str[i])
i++;
return (i);
}
char *ft_rev_print (char *str)
{
int i;
i = ft_strlen(str);
while (i)
{
write (1, (str +1), 1);
i--;
}
return (str); /*returning its argument */
}
int main(void) /*IT HAD TO WORK WITH THIS MAIN, DID NOT WROTE THIS MYSELF!*/
{
ft_rev_print("rainbow dash");
write(1, "\n", 1);
return (0);
}
I tried for ages to get it to work, but failed.. So now I'm breaking my head over this. What did i do wrong ? What did i miss?
Thanks in advance !