使用填充将整数转换为字符串

#include <stdio.h>
#include <limits.h>

#define MAXLINE 1000

void itoa(int n, char s[], int w);
void reverse(char s[]);

int main(void){
    char s[MAXLINE];
    int width;

    width = 11;
    itoa(INT_MIN, s, width);
    printf(%12d is converted to %s.\n, INT_MIN, s);
    itoa(123, s, width);
    printf(%12d is converted to %s.\n, 123, s);
    itoa(-321, s, width);
    printf(%12d is converted to %s.\n, -321, s);

    return 0;
}

void itoa(int n, char s[], int w){
    int i = 0, sign= n, remainder, k, j;

    do {
        remainder = n % 10;
        s[i++] = ((sign < 0) ? -remainder : remainder) + '0';
    } while (n /= 10);
    if (sign < 0)
        s[i++] = '-';

    while (i < w)
        s[i++] = ' ';

    s[i] = '\0';
    reverse(s);
}

void reverse(char s[]){
    int tmp;
    int j = 0;
    for (j = 0; s[j] != '\0'; ++j)
        ;
    --j;

    for (int i = 0; i < j; ++i, --j) {
        tmp = s[i];
        s[i] = s[j];
        s[j] = tmp;
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


声明一个计数循环
咕咕咕
#include <stdio.h> int main (void) { printf ("tutorial from book 2s.c om.\\n"); printf ("from book2s.com.\\n");
#include <stdio.h> int main (void) { printf ("Testing...\\n..1\\n...2\\n....3\\n"); return 0;//fromwww.y iiba i .co m
#include <stdio.h> int main (void) { int sum;//fromw w w. yiib ai . com sum = 50 + 25; printf ("The sum of 50 and 25 is %i\\n", sum);
#include <stdio.h> int main (void) { int value1, value2, sum; value1 = 50;/*ww w.yIi ba i. com*/ value2 = 25;
#include <stdio.h> int main()/* from w ww.yi ba I.c om*/ { int planets = 8; int friends = 6; int potterBooks = 7;
要求用户输入浮点,整数并将其打印在屏幕上,以下是代码的实现: #include <stdio.h>
C语言在屏幕上打印一条消息,参考以下实现代码: #include <stdio.h> int main() { printf("this is a test");
#include <stdio.h> int main(void) { int total; int cats; int dogs; int ponies; int others; cats = 4; dogs = 3;
#include <stdio.h> int main(void) { int ch; int ct = 0; while ((ch = getchar()) != EOF) ct++; printf("%d characters read\\n", ct);
switch 不使用 break 语句: #include <stdio.h> int main() { int iResponse = 0; printf("\\n1\\tSports\\n");
#include <stdio.h> int main() { char cResponse = '\\0'; printf("\\na\\tTurn the AC on\\n");
#include <stdio.h> int main() { int iSelection = 0; float fTransAmount = 0.0; float fBalance = 100.25;
#include <stdio.h> int main(void) { const float INCHES_PER_FEET = 12; float height; char name[40]; printf("What is your name?: ");
用双引号将其打印将其打印在20个字符宽的字段中,整个字段用引号括起,名称在字段的右端。在20字符宽的字段的左端打印它,整个字段用引号括起来。将其打印在比名称宽三个字符的字段中。
#include <stdio.h> int main(void) {// printf("a a\\n"); printf("a \\n a \\n");
#include <stdio.h> int main(void) { int ageyears;/* age in years */ int agedays;/* age in days*/ //fromwww .yi ib ai .co m
#include <stdio.h> void functionA(void); void functionB(void); int main(void){ functionA();/*fromwww.yi i bai.com*/
#include <stdio.h> void my_function(void);/* C function prototyping */ int main(void){ printf("main.\\n");