修改itoa函数,使得该函数可以接受三个参数。其中第三个参数为最小字符宽度。

来源:互联网 发布:淘宝商品资质怎么弄 编辑:程序博客网 时间:2024/05/16 09:52
#include <stdio.h>#include <limits.h>void itoa(int n, char s[], int width);void reverse(char s[]);int main(void) {    char buffer[20];    itoa(INT_MIN, buffer, 7);    printf("Buffer:%s\n", buffer);    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 % 10 + '0';        printf("%d %% %d + '0' = %d\n", n, 10, s[i-1]);    } while ((n /= 10) > 0);    if (sign < 0)        s[i++] = '-';    while (i < width )    /*  Only addition to original function  */        s[i++] = ' ';    s[i] = '\0';    reverse(s);}void reverse(char s[]) {    int c, i, j;    for ( i = 0, j = strlen(s)-1; i < j; i++, j--) {        c = s[i];        s[i] = s[j];        s[j] = c;    }} 
0 0
原创粉丝点击