gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:49:41 25 4
gpt4 key购买 nike

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

出于某种原因,它无法编译,它认为我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com