用单词C ++

时间:2017-01-21 14:27:04

标签: c++

#include <iostream>
#include <cstring>

using namespace std;

int main() {

    char main[10000] = {0};
    cin.getline(main, 10000);
    char clone [10000] = {0};
    int n;
    cin >> n;
    int counter = 0;
    int counter2 = 0;

    for (int z = 0 ; z < strlen(main); z++) {
        if (main[z] == '.' || main[z] == ',' || main[z] == ' ' || main[z] =='"' ||
            main[z] == '!' || main[z] == '?' || main [z] == ';')
            counter2++;
    }

    if (counter2 == 0) {
            if (strlen(main) < n) {
                n = n % strlen(main);
            }

            int b = strlen(main);

            for (int x = b - n; x < b  ; x++) {
                cout << main[x];
            }

            for (int y = 0; y < x - n; y++) {
                cout << main[y];
            }
    }

    for (int i = 0; i < strlen(main); i++) {
        clone[counter] = main[i];

        if(main[i] == '.' || main[i] == ',' || main[i] == ' ' || main[i] =='"' ||
           main[i] == '!' || main[i] == '?' || main [i] == ';') {

                int a = strlen(clone);

                if (counter < n) {
                    n = n % counter;
                }

                for (int x = a - n - 1; x < a - 1; x++) {
                    cout << clone[x];
                }

                for (int y = 0; y < x - n; y++) {
                    cout << clone[y];
                }
                cout << main[i];
                counter = 0;

                for (int z = 0; z < a; z++) {
                clone[z] = '\0';
                }
               }

           else {
                counter++;
           }
    }

    return 0;
}

我正在开发一个用文字切换字母的项目。例如,当n = 3时,有趣的单词将输出nnyfu。它只是移动所有字母n次。然而,当我尝试使用包含不同于字母的字符的长句子并使用相同类型的另一个字符(例如昏迷后跟空格)跟随它时,程序似乎崩溃了。我想知道是否有人可以帮助我,因为我整天都在拔头发。此外,对于带有几个单词的输入,以一个不同于字母的字符结尾,它不是必需的,但我似乎也无法弄明白(例如输入&#34; Hello my friends& #34;并且n = 3它将输出&#34; lloHe ym)。

0 个答案:

没有答案