Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
oorrja
GitHub Repository: oorrja/learntosolveit
Path: blob/master/languages/cprogs/Ex_4.12_recursive_itoa.c
1240 views
1
/* recursive version of itoa; that converts an integer string by calling a recursive routine */
2
3
#include<stdio.h>
4
#include<math.h>
5
6
#define MAXLEN 100
7
8
void itoa(int n,char s[]);
9
10
11
int main(void)
12
{
13
int n;
14
char s[MAXLEN];
15
16
n = 1723;
17
18
itoa(n,s);
19
20
printf("%s",s);
21
22
return 0;
23
}
24
25
void itoa(int n,char s[])
26
{
27
static int i;
28
29
if(n/10)
30
itoa(n/10,s);
31
else
32
{
33
i = 0;
34
if( n < 0)
35
s[i++]='-';
36
}
37
38
s[i++] = abs(n) % 10 + '0';
39
40
s[i] = '\0';
41
42
}
43
44
45