samir82show

c_ch01_ex19.c

Oct 10th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. /*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.*/
  2. #include <stdio.h>
  3. #define MAX 1000
  4. int reverse(char str[], char rev[], int length);
  5. int main()
  6. {
  7. int length = MAX, max;
  8. length = max = 0;
  9. char rev[MAX], str[MAX];
  10. while ((length = reverse (str, rev, MAX)) > 0)
  11. printf("%s\n", rev);
  12. return 0;
  13. }
  14. int reverse(char str[], char rev[], int length) {
  15. int c, i, j;
  16. i = j = 0;
  17. while ((c = getchar()) != EOF && c != '\n' && i < length)
  18. str[i++] = c;
  19. while (i > 0)
  20. rev[j++] = str[--i];
  21. rev[j] = '\0';
  22. return j;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment