lolamontes69

K+R Exercise4_12

Sep 7th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void itoa(int n, char s[]);
  5. void reverse(char s[]);
  6.  
  7. int main()
  8. {
  9.     int i, n;
  10.     char s[]="0";
  11.    
  12.     printf("Enter the integer :");
  13.     scanf("%d",&n);
  14.  
  15.     itoa(n,s);
  16.  
  17.     puts("Has rezuhlt...");
  18.     for(i=0; i<strlen(s); i++)
  19.         printf("%c",s[i]);
  20.     puts(" ");
  21.     return(0);
  22. }
  23.  
  24.  
  25. /* Convert an integer into a character string using recursion */
  26. void itoa(int n, char s[])
  27. {
  28.     static int i=0; /* use static variable for keeping count of position in string
  29.                      * this keeps its value through recursive calls to the function */
  30.     if(n<0) {
  31.         n = -n;
  32.         s[i++]='-';
  33.     }
  34.     if(n/10)
  35.         itoa(n/10,s);
  36.     s[i++] = (n % 10 + '0');
  37.     s[i]   = '\0';
  38. }
Advertisement
Add Comment
Please, Sign In to add comment