Guest User

Untitled

a guest
Apr 25th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. /*
  2. * Exercise 4-12. Adapt the ideas of printd to write a recursive version of
  3. * itoa; that is, convert an integer into a string by calling a recursive
  4. * routine.
  5. */
  6. #include <math.h>
  7. #include <string.h>
  8.  
  9.  
  10. void itoa(int n, char s[])
  11. {
  12. static int i;
  13.  
  14. if (n / 10)
  15. itoa(n / 10, s);
  16. else
  17. {
  18. i = 0;
  19. if (n < 0)
  20. s[i++] = '-';
  21. }
  22. s[i++] = abs(n) % 10 + '0';
  23. s[i] = '\0';
  24. }
  25.  
  26. int main(void)
  27. {
  28. int n = 100;
  29. char s[10];
  30. char exp[] = "100";
  31. itoa(n, s);
  32. if (strcmp(s, exp) == 0)
  33. printf("assert success.\n");
  34. else
  35. printf("assert fail.\n");
  36.  
  37. return 0;
  38. }
Add Comment
Please, Sign In to add comment