• 字符移位


    小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
    你能帮帮小Q吗?

    输入描述:

    输入数据有多组,每组包含一个字符串s,且保证:1<=s.length<=1000.

    输出描述:

    对于每组数据,输出移位后的字符串。

    输入例子:

    AkleBiCeilD

    输出例子:

    kleieilABCD

     1 #include "stdafx.h"
     2 #include <iostream>
     3 #include <string>
     4 #include <algorithm>
     5 #include<vector>
     6 using namespace std;
     7 int main()
     8 {
     9     string s;
    10     while (cin >> s)
    11     {
    12         int len = s.length();
    13         char ch;
    14         int tmp = 0;
    15 
    16         for (int i = 0; i < len-tmp; i++){
    17             if (isupper(s[i])){
    18                 ch = s[i];    //数组移位
    19                 for (int j = i; j < len-1; j++){
    20                         
    21                     s[j] = s[j + 1];
    22                 }
    23                 s[len-1] = ch;
    24                 tmp++;    //末尾是大写,不用遍历了。
    25                 i--;    //移位后,要判断该位置的字符是不是大写。因此不能让索引加1
    26             }
    27         }
    28         cout << s << endl;
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    Spring aop 实现异常拦截
    drf 视图家族
    算法与数据结构
    接口
    Kubernetes
    drf
    drf 序列化
    drf 内部模块
    drf 接口
    vue
  • 原文地址:https://www.cnblogs.com/yuguangyuan/p/5814782.html
Copyright © 2020-2023  润新知