将int转化为string的问题,附加条件是字符数组有最小宽度,不够就以空格补齐
程序代码:
#include <stdio.h> #include <string.h> void reverse(char s[]) {int i,j; char c; for(i=0,j=strlen(s)-1;i<j;i++,j++) {c=s[i];s[i]=s[j];s[j]=c;} } void itoa(int n,char s[],int w) {int i=0,sign; if((sign=n)<0)n=-n; do {s[i++]=n%10;}while(n/=10>0); if(sign<0)s[i]='-'; if(i<w) for(;i<=w;i++) s[i]=' '; s[i]='\0'; reverse(s); } int main(void) {int n=994,w=5; char s[100]; itoa(n,s,w); printf(s); }//where is the problem?