Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Exercise 1-19. Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time.*/
- #include <stdio.h>
- #define MAX 1000
- int reverse(char str[], char rev[], int length);
- int main()
- {
- int length = MAX, max;
- length = max = 0;
- char rev[MAX], str[MAX];
- while ((length = reverse (str, rev, MAX)) > 0)
- printf("%s\n", rev);
- return 0;
- }
- int reverse(char str[], char rev[], int length) {
- int c, i, j;
- i = j = 0;
- while ((c = getchar()) != EOF && c != '\n' && i < length)
- str[i++] = c;
- while (i > 0)
- rev[j++] = str[--i];
- rev[j] = '\0';
- return j;
- }
Advertisement
Add Comment
Please, Sign In to add comment