ch07s048.c

来自「稀疏矩阵、链表、图、队列、二叉树、多叉树、排序、遗传算法等的实现」· C语言 代码 · 共 38 行

C
38
字号
#include <stdio.h>
#include <assert.h>

#define  SWAP(x, y)  x^= y^= x;

int
main()
{
    char *s="uvwxyz";
    char *t;
    unsigned long a, b, c;
    int i;

    printf("String: %s (%d entries).\n"
           "Change which character to '1'? ", s,
           strlen(s));
    scanf("%d", &a);

    assert((0 <= a) && (a < strlen(s)));
    s[a]= '1';

    printf("Original changed to %s\n", s);
    printf("Reverse which range of characters (from-to)? ",
           b, c);

    scanf("%lu%lu", &a, &b);
    assert(a<=b);

    t= malloc(strlen(s+1));

    strcpy(t, s);
    for (i=b; i<(b+c)/2; i++)
        SWAP(t[b+i], t[c-i]);

    printf("Result of reverse is %s -> %s\n", s, t);
    return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?