lundi 2 février 2015

Modify program to encrypt both uppercase and lowercase input

I have to write code for a random cipher problem. I have done it, but the program transforms only uppercase or lowercase (depending on what I choose) letters as input. What should I change so that the program transforms both upper and lower-case letters?


code:



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

}
}




Aucun commentaire:

Enregistrer un commentaire