lolamontes69

K+R Exercise4_13

Sep 7th, 2014
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void reverse(char s[]);
  5.  
  6. int main()
  7. {
  8.     int i, c;
  9.     char s[255];
  10.    
  11.     printf("Enter a string >");
  12.     i=0;
  13.     while((c=getchar())!='\n') {
  14.         s[i]=c;
  15.         ++i;
  16.     }                      
  17.     s[i]='\0';
  18.  
  19.     reverse(s);
  20.     for(i=0; i<strlen(s); i++)
  21.         printf("%c",s[i]);
  22.     puts(" ");
  23.     return(0);
  24. }
  25.  
  26. void reverse(char s[])
  27. {
  28.     static int i=0;
  29.     char temp[2];
  30.  
  31.     int SLEN = strlen(s);
  32.     int first=i;
  33.     int last=SLEN-i-1;
  34.    
  35.     if(last<=first)
  36.         return;
  37.  
  38.     temp[0] = s[first];
  39.     s[first] = s[last];
  40.     s[last] = temp[0];
  41.     i++;
  42.     reverse(s);
  43.     s[SLEN] = '\0';
  44. }
Advertisement
Add Comment
Please, Sign In to add comment