rot13.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Assignment name : rot_13
  2. Expected files : rot_13.c
  3. Allowed functions: write
  4. --------------------------------------------------------------------------------
  5. Write a program that takes a string and displays it, replacing each of its
  6. letters by the letter 13 spaces ahead in alphabetical order.
  7. 'z' becomes 'm' and 'Z' becomes 'M'. Case remains unaffected.
  8. The output will be followed by a newline.
  9. If the number of arguments is not 1, the program displays a newline.
  10. Example:
  11. $>./rot_13 "abc"
  12. nop
  13. $>./rot_13 "My horse is Amazing." | cat -e
  14. Zl ubefr vf Nznmvat.$
  15. $>./rot_13 "AkjhZ zLKIJz , 23y " | cat -e
  16. NxwuM mYXVWm , 23l $
  17. $>./rot_13 | cat -e
  18. $
  19. $>
  20. $>./rot_13 "" | cat -e
  21. $
  22. $> */
  23. #include <unistd.h>
  24. void ft_rot13(char *str)
  25. {
  26. int i;
  27. i = 0;
  28. while (str[i])
  29. {
  30. if ((str[i] >= 'A' && str[i] <= 'M') || (str[i] >= 'a' && str[i] <= 'm'))
  31. {
  32. str[i] += 13;
  33. write(1, &str[i], 1);
  34. }
  35. else if ((str[i] > 'M' && str[i] <= 'Z') || (str[i] > 'm' && str[i] <= 'z'))
  36. {
  37. str[i] -= 13;
  38. write(1, &str[i], 1);
  39. }
  40. else
  41. {
  42. write(1, &str[i], 1);
  43. }
  44. i++;
  45. }
  46. }
  47. int main(int argc, char **argv)
  48. {
  49. if (argc != 2)
  50. write(1, "\n", 1);
  51. else
  52. {
  53. ft_rot13(argv[1]);
  54. write(1, "\n", 1);
  55. }
  56. return (0);
  57. }