1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- /* Assignment name : rot_13
- Expected files : rot_13.c
- Allowed functions: write
- --------------------------------------------------------------------------------
- Write a program that takes a string and displays it, replacing each of its
- letters by the letter 13 spaces ahead in alphabetical order.
- 'z' becomes 'm' and 'Z' becomes 'M'. Case remains unaffected.
- The output will be followed by a newline.
- If the number of arguments is not 1, the program displays a newline.
- Example:
- $>./rot_13 "abc"
- nop
- $>./rot_13 "My horse is Amazing." | cat -e
- Zl ubefr vf Nznmvat.$
- $>./rot_13 "AkjhZ zLKIJz , 23y " | cat -e
- NxwuM mYXVWm , 23l $
- $>./rot_13 | cat -e
- $
- $>
- $>./rot_13 "" | cat -e
- $
- $> */
- #include <unistd.h>
- void ft_rot13(char *str)
- {
- int i;
-
- i = 0;
- while (str[i])
- {
- if ((str[i] >= 'A' && str[i] <= 'M') || (str[i] >= 'a' && str[i] <= 'm'))
- {
- str[i] += 13;
- write(1, &str[i], 1);
- }
- else if ((str[i] > 'M' && str[i] <= 'Z') || (str[i] > 'm' && str[i] <= 'z'))
- {
- str[i] -= 13;
- write(1, &str[i], 1);
- }
- else
- {
- write(1, &str[i], 1);
- }
- i++;
- }
- }
- int main(int argc, char **argv)
- {
- if (argc != 2)
- write(1, "\n", 1);
- else
- {
- ft_rot13(argv[1]);
- write(1, "\n", 1);
- }
- return (0);
- }
|