#include <stdio.h>
#include <string.h>
#define MAX 50
/*
@功能:定义一个函数,其功能是“将字符串a中下标值为偶数的元素由小到大排序,其他元素不变“
@参数:指向要排序的字符串的指针
@返回值:无
*/
void sort(char *a)
{
    int len = strlen(a), i, j;
    char c;
    for (i = 0; i < len; i += 2)
   //这个地方应该是 i < len - 2; 否则数组会越界的。
    {
        for (j = i + 2; j < len; j += 2)
        {
            if (*(a + i) > *(a + j))
            {
                c = *(a + i);
                *(a + i) = *(a + j);
                *(a + j) = c;
            }
        }
    } 
}
/*
@功能:主函数,调用sort(char *a)对输入的字符串进行排序
*/
int main(void) 
{
    char c[MAX];
    printf("Please input a string with max length %d:\n", MAX);
    scanf("%s", c);
    if (strlen(c) > 50)
    {
        printf("The string that you input is too long!");
        
        exit(0);
    }
    sort(c);
    printf("After sort:\n");
    puts(c);
    return 0;
}
[[it] 本帖最后由 mqh21364 于 2008-4-14 15:21 编辑 [/it]]