c++ - 我想创建一个函数,我可以在其中输入一个字符串,它会输出一个困惑版本的字符串

标签 c++ random

我似乎无法弄清楚哪里出了问题

出于某种原因,它无法编译,它认为我的 jumbleString 函数有问题

#include <iostream>
#include <iomanip>
#include <string>
#include <istream>

using namespace std;

int main ()
{
    int lengthofstring, x, countWords(string str), countConsonant(string str, int), consonant, jumbleString(string str);
    string str, str2, wordone;

    char options;
    cout << "Please enter a word, a sentence, or a string of numbers." << endl;

    getline(cin, str);

    //cin >> str;

    lengthofstring = str.length(); 
    str2=str;

    bool another= true;

    while (another) 
    {
        cout << '\n' << "USE THIS MENU TO MANIPULATE YOUR STRING" << endl;
        cout << "---------------------------------------" << endl;
        cout << "1) Inverse String" << endl;
        cout << "2) Reverse String" << endl;
        cout << "3) To Uppercase" << endl;
        cout << "4) Jumble String" << endl;
        cout << "5) Count Number Words" << endl;
        cout << "6) Count Consonants" << endl;
        cout << "7) Enter a Different String" << endl;
        cout << "8) Print the String" << endl;
        cout << "Q) Quit" << endl;

        cin >> options;

        switch (options)
        {
        case '1':
            for (x = 0; x < lengthofstring; x++)
            {
                if (islower(str[x]))
                    str[x] = toupper(str[x]);
                else if (isupper(str[x]))
                    str[x] = tolower(str[x]);

            }
            cout<< str;
            break;
        case '2':
            for (x = 0; x < lengthofstring; x++)
            {
                str2[x] = str[lengthofstring-1-x];
            }
            cout<< str2;
            break;
        case '3':
            {
                for (x = 0; x < lengthofstring; x++)
                { 
                    if (islower(str[x]))
                        str[x] = toupper(str[x]);
                }
                cout<< str;
            }
            break;
        case '4':
            jumbleString(str);
            break;

        case '5':
            cout << countWords(str);
            break;
        case '6': 
            consonant = 0;
            cout<< countConsonant(str, consonant);
            break;
        case '7':
            cout << "Please enter another word, a sentence, or a string of numbers." << endl;
            cin.ignore();
            getline(cin, str);
            cout << str <<endl;
            break;
        case '8':
            cout<< str2;
            break;
        case 'q':
            another = false;
            break;
        }
    }

    cin.get();
    cin.get();
    return 0;
}

void jumbleString(string str)
{
    int length = str.length();
    int  j, k;

    for(int i = 0; i < length; j++)
    {
        k = rand() % length;
        j = rand() % length;
        char c = str[j];
        str[j] = str[k];
        str[k] = c;
    }

    cout << str<<endl;
}

int countWords(string str)
{
    int length = str.length();
    int words = 1;
    for(int size = 1; length > size; size++)
    {
        if (str[size] == ' ' && str[size-1] != ' ')
            words++;
    }
    if (str[0] == ' ')
        words--;
    return words;
}
int countConsonant(string str, int consonant)
{
    int length = str.length();
    consonant = 0;

    for (int i = 0; i < length; i++)
    {
        if (str[i] != 'a' && str[i] != 'e' && str[i] != 'i' && 
            str[i] != 'o'&& str[i] != 'u' && str[i] != 'A' && str[i] != 'E' 
            && str[i] != 'I' && str[i] != 'O' && str[i] != 'U' && str[i] != ' '&& str[i] != '1'
            && str[i] != '2' && str[i] != '3' && str[i] != '4' && str[i] != '5' && str[i] != '6'
            && str[i] != '7' && str[i] != '8' && str[i] != '9' && str[i] != '0')
            consonant = consonant + 1;
    }
    return consonant;
}

最佳答案

问题是在循环中改变 i(我猜你是想改变 k):
如果你确实想设置k,把i = rand() % length;改成k = rand() % length;
另外,你的问题是排列问题的一个变体,Fisher-Yates解决。我建议您看看它,您可能会通过使用它获得更好的“随机性”。

关于c++ - 我想创建一个函数,我可以在其中输入一个字符串,它会输出一个困惑版本的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5925960/

相关文章:

c++ - appendChild xml c++ 无法按需工作

c++ - 使用指针复制构造函数

c++ - CUDA 似乎无法编译

用于生成随机 53 位数字的 C 函数

c++ - 如何随机生成平均值为 0、标准差为 1 的 double 数组?

c++ - 继承模板类的构造函数

c++ - 随机数生成器机制

java - 如何在java中为新用户自动生成登录ID?

javascript - 在 JS 的循环中返回一个随机数,但从不相同

c++ - 用C++实现一棵树