1. 程式人生 > >C-凱撒加密

C-凱撒加密

//將每個字母用固定距離後的字母替代(使用者輸入)
//時間加密和解密

#include<stdio.h>

int main(void) {
	char msg[20];
	char c;
	int s,i;
	for ( i = 0; i <= 20; i++) {
		msg[i] = ' ';
	}
	i = 0;
printf("Enter a message: ");
while (i<20&&(c = getchar()) != '\n') {
	msg[i] = c;
	i++;
}
printf("Enter shift amount(1-25): ");
scanf_s("%c", &s);

for (i = 0; i < 20; i++) {
	if (msg[i] > 'A'&&msg[i]<'Z' ) {
		msg[i] = (msg[i] + s-'A') % 26 + 'A';
	}
	if (msg[i] > 'a'&&msg[i] < 'z') {
		msg[i] = (msg[i] + s - 'a') % 26 + 'a';
	}
}

printf("Encrypted message: ");
for (i = 0; i < 20; i++) {
	printf("%c", msg[i]);
}
return 0;

}
引發異常orz引發異常