把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,不能申请额外的空间

腾讯一面试题,采用冒泡排序的思想,大写字母向后移动,小写字母向前移动,时间复杂度为O(N^2)。

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char str[100];
    while (scanf("%s", str) != EOF)
    {
        int n, j;
        int nLen = strlen(str);
        for (int i = nLen - 1; i > 0; i--)
        {
            if (islower(str[i]))
            {
                j = i;
                while(islower(str[j]) && j >= 1)
                {
                    j--;
                }
                if (j == 0 && islower(str[j]))
                {
                    break;
                }
                if (isupper(str[j]))
                {
                    char temp = str[j];
                    for (int k = j; k < i; k++)
                    {
                        str[k] = str[k + 1];
                    }
                    str[i] = temp;
                }
            }
        }
        printf("%s
", str);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/lzmfywz/p/3293266.html