Path: blob/master/languages/cprogs/Ex_3.6_itoa-3.c
1240 views
/* a function of itoa, which accepts the third argument as the width of the number.1the string representation is padded with blanks in the left to get the required width */23#include<stdio.h>4#include<string.h>56#define MAXLIMIT 10078void itoa(int n,char s[],int w);9void reverse(char s[]);1011int main(void)12{13int number,width;14char str[MAXLIMIT];1516number= -343565;17width=10;1819itoa(number,str,width);2021printf("%s",str);2223return 0;24}2526void itoa(int n,char s[],int w)27{28int i,sign;2930if((sign=n) < 0)31n = -n;32i=0;3334do35{36s[i++] = (n %10) + '0';3738}while((n/=10)>0);3940if(sign <0)41s[i++]='-';4243while(i<w)44s[i++]=' ';4546s[i]='\0';4748reverse(s);49}5051void reverse(char s[])52{53int i,j,c;5455for(i=0,j=strlen(s)-1;i<j;i++,j--)56c=s[i],s[i]=s[j],s[j]=c;57}58596061