我正在将字符传递到字符串中并使用函数混淆字符串中的字母,但是从主顶部的数组中抓取的字符正在作为符号的重音符号打印并且数字的%符号,但是字母不受影响。帮助表示赞赏:)打印奇怪值的特殊字符和数字字符
#include <iostream>
#include <string>
using namespace std;
void scrambleString(string str)
{
int x = str.length();
for (int y = x; y > 0; y--)
{
int pos = rand() % x;
char tmp = str[y - 1];
str[y - 1] = str[pos];
str[pos] = tmp;
}
cout << str;
}
int main()
{
// Arrays for RNG to work with //
char letters[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
char symbols[] = { '!', '£', '$', '%', '&', '*', '@', '~' };
char numbers[] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
// Other Variables //
int letterIn;
int specialIn;
int numberIn;
string randomPass;
string holdString;
cout << "No of Letters: ";
cin >> letterIn;
cout << "No of Special char: ";
cin >> specialIn;
cout << "No of Numbers: ";
cin >> numberIn;
// Letters
for (int i = 0; i < letterIn; i++)
{
int RNG = rand() % 26;
char holdChar = letters[RNG];
// Convert to String
holdString.insert(i, 1, holdChar);
}
// Symbols
for (int i = 0; i < specialIn; i++)
{
int RNG = rand() % 8;
char holdChar = symbols[RNG];
// Convert to String
holdString.insert(letterIn, 1, holdChar);
}
// Numbers
for (int i = 0; i < numberIn; i++)
{
int RNG = rand() % 8;
char holdChar = symbols[RNG];
// Convert to String
holdString.insert(letterIn+specialIn, 1, holdChar);
}
cout << holdString << endl;
scrambleString(holdString);
}
请问您可以发布一个示例输出吗? –
请注意,''''没有在ASCII中定义;这个字符可能需要很宽才能正确解释为Unicode。为了简单起见,最好完全省略它。 – cdhowie
这个例子产生我期望的输出,如果我省略''''''并且减少'//符号'中的模匹配。考虑使用'sizeof'而不是常量。 –