rev_print.c 870 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* Assignment name : rev_print
  2. Expected files : rev_print.c
  3. Allowed functions: write
  4. --------------------------------------------------------------------------------
  5. Write a program that takes a string, and displays the string in reverse
  6. followed by a newline.
  7. If the number of parameters is not 1, the program displays a newline.
  8. Examples:
  9. $> ./rev_print "zaz" | cat -e
  10. zaz$
  11. $> ./rev_print "dub0 a POIL" | cat -e
  12. LIOP a 0bud$
  13. $> ./rev_print | cat -e
  14. $ */
  15. #include <unistd.h>
  16. #include <stdio.h>
  17. int ft_strlen(char *str)
  18. {
  19. int i;
  20. i = 0;
  21. while(str[i])
  22. {
  23. i++;
  24. }
  25. return (i);
  26. }
  27. char *ft_revprint(char *str)
  28. {
  29. int count;
  30. count = ft_strlen(str);
  31. while(count > 0)
  32. {
  33. write(1, &str[count - 1], 1);
  34. count--;
  35. }
  36. write(1, "\n", 1);
  37. return (str);
  38. }
  39. int main(int argc, char **argv)
  40. {
  41. if (argc != 2)
  42. write(1, "\n", 1);
  43. else
  44. ft_revprint(argv[1]);
  45. }