我想创建一个函数,我可以在其中输入一个字符串,它会输出一个混乱的字符串版本

i wanted to create a function where i can enter a string and it would output a a jumbled version of the string

本文关键字:一个 字符串 输出 混乱 版本 函数 我可以 在其中 输入 创建      更新时间:2023-10-16

我似乎无法弄清楚出了什么问题

由于某种原因,它无法编译,并且认为我的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;
另外,你的问题是排列问题的变体,费舍尔-耶茨解决了这个问题。我建议看看它,你可能会通过使用它获得更好的"随机性"。

你在这里错误地使用了循环变量 i 两次。此外,如果您想要真正随机混杂字符串,您可能希望为随机数生成器播种。

对于在 c++ 中执行此操作的惯用方法,您可以使用标准算法执行此操作,如下所示:

#include <iostream>
#include <algorithm>
#include <ctime>
#include <cstdlib>
int main(void){
        srand ( unsigned ( time (NULL) ) );//seed the random shuffle
        std::string test = "abcdef";
        std::cout << "original string: " << test << std::endl;
        std::random_shuffle(test.begin(),test.end());
        std::cout << "shuffled string: " << test << std::endl;
        return 0;
}

你用 i, j 作为你的两个随机索引,而这些应该是 j, k。

它应该是:

j = rand() % length;
k = rand() % length;

您将i用作循环变量,但同时在循环中为其分配一个随机值。

一个可能的解决方案是根本不使用两个随机,而是迭代变量i本身[在线示例]。

for(int i = 0; i < length; i++)
{
    j = i + (rand() % (length-i));
    char c = str[j];
    str[j] = str[i];
    str[i] = c;
}
相关文章: