i wanted to create a function where i can enter a string and it would output a a jumbled version of the string
i cant seem to figure out what wrong
for some reason it wont compile and it think theres a problem on my jumbleString
function
#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) Jum开发者_如何学编程ble 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;
}
the problem is changing i inside the loop (I guess you meant to change k):
if you did mean to set k, change i = rand() % length;
into k = rand() % length;
also, your question is a variant of the permutation problem, which Fisher-Yates solves. I would suggest looking at it, you will probably get better "randomness" by using it.
You are mistakenly using the loop variable, i , twice here. Also you might want to seed the random number generator if you want truly random jumbling of the strings.
For an idiomatic way of doing this in c++ you can use the standard algorithms to do this as follows:
#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;
}
You are using i, j for your two random indices whereas these should be j, k.
It should be:
j = rand() % length;
k = rand() % length;
You are using i
as loop variable but at the same time assign a random value to it within the loop.
A possible solution would be not to use two randoms at all but instead the iterating variable i
itself [online example].
for(int i = 0; i < length; i++)
{
j = i + (rand() % (length-i));
char c = str[j];
str[j] = str[i];
str[i] = c;
}
精彩评论