lolamontes69

K+R Exercise1_19

Sep 5th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.65 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MAXLINE 1000
  4.  
  5. int getline(char line[], int maxline);
  6. void reverse(char line[]);
  7.  
  8. int main()
  9. {
  10.     int len, i;
  11.     char line[MAXLINE];
  12.  
  13.     for(i=0; i<=MAXLINE; ++i)
  14.         line[i]='\0';
  15.     while(gets(line)) {               // EOF returns 0
  16.         reverse(line);
  17.         for(i=0; i<=MAXLINE; ++i)
  18.             line[i]='\0';
  19.     }
  20.     return(0);
  21. }
  22.  
  23.  
  24. void reverse(char s[])
  25. {
  26.     int i, j;
  27.  
  28.     for(i=0; i<MAXLINE-2 && s[i]!='\0'; ++i)
  29.         ;                             // Just count length of s
  30.     for(j=i-1; j>=0; --j)             // Then print backwards
  31.         printf("%c",s[j]);
  32.     printf("\n");
  33. }
Advertisement
Add Comment
Please, Sign In to add comment