Exercise 3-6. Write a version of itoa that accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough.
/* Exercise 3-6. Write a version of itoa that accepts three arguments instead of two. The third argument is a minimum field width; the converted number must be padded with blanks on the left if necessary to make it wide enough.*/#include<stdio.h>#include<string.h>#include<limits.h>#define LIMIT 100#define VERSION 2 /* change the version number to either 1 or 2 */voidreverse(chars[]);voiditoa_V01(intn,chars[]);voiditoa_V02(intn,chars[],intfield_width);intmain(){intn=INT_MIN;intfield_width=20;chars[LIMIT];if(VERSION==1){itoa_V01(n,s);printf("'%s' (V01)\n",s);}elseif(VERSION==2){itoa_V02(n,s,field_width);printf("'%s' (V02)\n",s);}elseprintf("Entered version number is not correct\n");return0;}/* reverse: reverse string s in place */voidreverse(chars[]){intc,i,j;for(i=0,j=strlen(s)-1;i<j;i++,j--)c=s[i],s[i]=s[j],s[j]=c;}/* itoa_V01: convert n to characters in s */voiditoa_V01(intn,chars[]){inti,sign;if((sign=n)<0)/* record sign */n=-(n+1);/* make n positive (and add 1 to avoid overflow for INT_MIN)*/i=0;do{/* generate digits in reverse order */s[i++]=n%10+'0';/* get next digit */}while((n/=10)>0);/* delete it */if(sign<0){s[i++]='-';s[0]++;/* 0th element is Incremented by 1 to compensate for the addition that we did above */}s[i]='\0';reverse(s);}/* itoa_V02: convert n to characters in s -> minimum field width is added as a third argument */voiditoa_V02(intn,chars[],intfield_width){inti,sign;if((sign=n)<0)/* record sign */n=-(n+1);/* make n positive (and add 1 to avoid overflow for INT_MIN)*/i=0;do{/* generate digits in reverse order */s[i++]=n%10+'0';/* get next digit */}while((n/=10)>0);/* delete it */if(sign<0){s[i++]='-';s[0]++;/* 0th element is Incremented by 1 to compensate for the addition that we did above */}while(i<field_width)s[i++]=32;/* padded with blanks on the left to make it wide enough (i.e, field_width)*/s[i]='\0';reverse(s);}