一道旋转字符串的程序

img


这道需要程序先旋转字符串在进行奇数位选取
希望大家能够用容易理解的c++语言进行编写

这就是字符串的常规操作啊,简单处理就是写一个左移一个字符的函数,以及一个右移一个字符的函数。然后循环调用

#include <iostream>
using namespace std;
#include <string.h>
void moveleft(char *s,int len)
{
    char c = *s;
    for(int i=1;i<len;i++)
        s[i-1] = s[i];
    s[len-1] = c;
}

void moveright(char *s,int len)
{
    char c = *(s+len-1);
    for(int i=len-2;i>=0;i--)
        s[i+1] = s[i];
    s[0] = c;
}

int main()
{
    int N;
    char s[16] = {0},opr,M,len;
    cin>>N;
    for(int i=0;i<N;i++)
    {
        cin>>s>>opr>>M;
        len = strlen(s);
        M = M%len;
        switch(opr)
        {
        case 'R':
            {
                for(int j=0;j<M;j++)
                    moveright(s,len);
            }
            break;
        case 'L':
            {
                for(int j=0;j<M;j++)
                    moveleft(s,len);
            }
            break;
        }
        for(int j=0;j<len;j+=2)
            cout<<s[j];
        cout<<endl;
    }
    return 0;
}

#include <iostream>
#include <string>

using namespace std;

int main()
{
    char ch[20];
    int i, j, b, n, m[20];
    string str[20];
    string s1, s2;
    cin >> n;
    for(i = 0; i < n; i++) {
        cin >> str[i] >> ch[i] >> m[i];
    }
    for(i = 0; i < n; i++) {
        if(ch[i] == 'L') {
            b = m[i] % str[i].size();   // 求实际需要左移的位数
            s1 = str[i].substr(0, b);   // 取出字符串左移到后面的子串 如abcdef左移2,则取出ab
            s2 = str[i].substr(b, str[i].size() - b);   // 取出字符串左移到前面的子串 如abcdef左移2,则取出cdef
            str[i] = s2 + s1;
        }
        else if(ch[i] == 'R') {
            b = m[i] % str[i].size();   // 求实际需要右移的位数
            s1 = str[i].substr(0, str[i].size() - b);   // 取出字符串右移到后面的子串 如abcdef右移2,则取出abcd
            s2 = str[i].substr(str[i].size() - b, str[i].size()); // 取出字符串右移到前面的子串 如abcdef右移2,则取出ef
            str[i] = s2 + s1;
        }
    }
    for(i = 0; i < n; i++) {
        for(j = 1; j <= (int)str[i].size(); j++) {
            if(j % 2 != 0) {
                cout << str[i].at(j - 1);
            }
        }
        if(i < n - 1) {
            cout << endl;
        }
    }

    return 0;
}

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
    int N, n;
    string str, M;
    cin >> N;
    while (N--) {
        cin >> str;
        cin >> M >> n;
        //如果n大于字符串长度取余确保不越界
        int len = str.size();
        n %= len;
        if (M == "R") {
            //reverse 为<algorithm>库中的字符串逆转方法
            //例abcdef
            reverse(str.begin(), str.end());
            //cout<<str<<endl;   输出结果 fedcba
            reverse(str.begin(), str.begin() + n);
             //cout<<str<<endl;  输出结果 efdcba
            reverse(str.begin() + n, str.end());
             //cout<<str<<endl;  输出结果 efabcd
            for (int i = 0; i < len; i += 2) {
                cout << str[i];
            }
            cout << '\n';
        }
        else if (M == "L") {
            reverse(str.begin(), str.begin() + n);
            reverse(str.begin() + n, str.end());
            reverse(str.begin(), str.end());
            for (int i = 0; i < len; i += 2) {
                cout << str[i];
            }
            cout << '\n';
        }
        else {
            for (int i = 0; i < len; i += 2) {
                cout << str[i];
            }
            cout << '\n';
        }
    }
    return 0;
}
#include <iostream>
#include <string.h>
#include <sstream>
using namespace std;

int main()
{
    int N, M, len;
    char s[16] = {0}, opr;
    string resultStr;
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        cin >> s >> opr >> M;
        len = strlen(s);
        M = M % len;
        if (opr == 'R')
        {
            std::ostringstream ss;
            string temp = s;
            ss << temp.substr(len - M, M)<< temp.substr(0, len - M); //将字符串分为两部分,交换顺序
            resultStr = ss.str();
        }
        else if (opr == 'L')
        {
            std::ostringstream ss;
            string temp = s;
            ss << temp.substr(M, len - M) << temp.substr(0, M);//将字符串分为两部分,交换顺序
            resultStr = ss.str();
        }
        else // 题干要求如果opr不时R/L原样输出字符
        { 
            resultStr=s;
        }
        for (int j = 0; j < len; j += 2)
            cout << resultStr[j];
        cout << endl;
    }
    return 0;
}