Originally Posted by
Shurik
If it as I understand is
Caesar cipher and letters in Alphabet are in alphabet order, then:
Code:
for(int i=0; i<n;i++) //n is the length of the string
{
for(int j=0;j<LIMIT;j++) //Limit is the number of characters in the alphabet.
{
if(UserString2[i]==Alphabet[j])
{
UserString2[i] =Alphabet[(j+k) % LIMIT];//k is the amount of spaces the user wants the character to be moved to to be switched to another character.
}
}
}
I don't understand the second nested loop here. The shift is done in one addition or subtraction, for each letter.
Code:
//encrypt cipher n is the secret shift number (sssh!) ;)
n= 5;
for(i=0;i<len;i++) { //len=strlen(cipher)
if(isalpha(cipher[i])) {
if(cipher[i]+n <= 'z')
cipher[i]=cipher[i]+n;
else
cipher[i]=cipher[i]+n-26;
}else
cipher[i]=clear[i]; //don't change non alpha char's
}
}
printf("\n\nclear: %s\n\ncipher: %s\n",clear,cipher);