【发布时间】:2015-02-02 15:29:00
【问题描述】:
我必须为随机密码问题编写代码。我已经做到了,但是程序只转换大写或小写(取决于我选择的)字母作为输入。我应该更改什么以使程序同时转换大小写字母?
代码:
srand(time(0)); //seed for rand()
static char alphabet[]="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alph="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const int LENGTH=sizeof(alphabet)-1;
int r;
char temp;
for (unsigned int i=0; i<LENGTH; i++) //loop which shuffles the array
{
r=rand() % LENGTH;
temp=alphabet[i];
alphabet[i] = alphabet[r];
alphabet[r]=temp;
}
string text;
getline (cin, text);
for (unsigned int i=0; i<text.length(); i++) //loop to encrypt
{
if (isalpha(text[i]))
{
text[i]=alphabet[text[i] - 'A']; //index alphabet with the value of text[i], adjusted to be in the range 0-25
}
}
【问题讨论】:
标签: c++ encryption random shuffle alphabet