Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Exercise 4-12. Adapt the ideas of printd to write a recursive version of
- * itoa; that is, convert an integer into a string by calling a recursive
- * routine.
- */
- #include <math.h>
- #include <string.h>
- void itoa(int n, char s[])
- {
- static int i;
- if (n / 10)
- itoa(n / 10, s);
- else
- {
- i = 0;
- if (n < 0)
- s[i++] = '-';
- }
- s[i++] = abs(n) % 10 + '0';
- s[i] = '\0';
- }
- int main(void)
- {
- int n = 100;
- char s[10];
- char exp[] = "100";
- itoa(n, s);
- if (strcmp(s, exp) == 0)
- printf("assert success.\n");
- else
- printf("assert fail.\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment