I am trying to create a Caesar cipher, so I declared a function that pushes every character 13 times. And then another function that takes a string and then encrypts or decrypts it, I did that with the for loop. But the problem is that when I run the programm this is what comes out:
Original: This is the Original text
encrypted: QnÇ vÇü qre Bevtvanyüràü
decrypted: DaÇ âÇü der Orâüânaåüeàü
Does anybody have an idea, what might be causing this?
#include <stdio.h>
#include <stdlib.h>
int shift = 13;
char shiftchar(char ch){
if( ((ch > 64) && (ch< 91)) || ((ch > 96) && (ch < 123)) ){
ch = ch + shift;
if(ch > 90 && ch < 104){
ch = ch - 90 +64;
}
else if(ch > 122 && ch < 136){
ch = ch -122 +96;
}
}
else{
ch = ch;
}
}
void cipher (char str[]){
for( int i = 0; str[i] != 0; ++i){
str[i] = shiftchar(str[i]);
}
}
int main(void){
char str[25] = "This is the original text";
printf("Original: ");
printf("%s\n", str);
cipher(str);
printf("encrypted: ");
printf("%s\n", str);
cipher(str);
printf("decrypted: ");
printf("%s\n", str);
}