2014年05月19日

来源:互联网 发布:呱呱社区软件 编辑:程序博客网 时间:2024/03/29 22:31

practice 3-6: write a funtion itoa(n,s,width) that couldreceive the width ,which is the legal least charactor width.if s[]'s width under the limits,make some blank in it tothe limits.

#include

void itoa(int n,char s[],int width);
void reverse(char s[]);
main()
{
 int number=17,b1=2,b2=8,b3=16;
 char str[10];

 printf("original digit:%d\n",number);
 itoa(number,str,b2);
 printf("change:%s",str);
 


 return 0;
}

void itoa(int n,char s[],int width)
{
 int i,sign;

 if((sign=n)<0)
  n=-n;
 i=0;
 do{
  s[i++]=n+'0';
  }while((n/=10)>0);
 if(sign<0)
  s[i++]='-';
 while(i
  s[i++]=' ';
 s[i]='\0';

 reverse(s);
}
void reverse(char s[])
{
 int c,i,j;
 for(i=0,j=strlen(s)-1;i
 {
  c=s[i];s[i]=s[j];s[j]=c;
 }
}

0 0
原创粉丝点击