CAESAR加密 操練
先貼Caesar Cipher的wikipedia.#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[]) {
//check the argument
if (argc != 2) {
printf("You should input ONE,and ONLY ONE argument.\n");
return 1;
}
int KEY = atoi(argv[1]);
printf("Plain Text:");
char *text=GetString();
char *tmp=(char *)malloc(sizeof(char) * strlen(text));
memcpy(tmp,text,strlen(text));
int i=0;
while(tmp[i] != '\0') {
if ( isalpha(tmp[i])) {
if ( isupper(tmp[i]) ) {
tmp[i]=(tmp[i] - 'A' + KEY)%26 + 'A';
} else {
tmp[i]=(tmp[i] - 'a' + KEY)%26 + 'a';
}
}
i++;
}
printf("Cypher: %s\n",tmp);
free(tmp);
return 0;
}
#include <cs50.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[]) {
//check the argument
if (argc != 2) {
printf("You should input ONE,and ONLY ONE argument.\n");
return 1;
}
int KEY = atoi(argv[1]);
printf("Plain Text:");
char *text=GetString();
char *tmp=(char *)malloc(sizeof(char) * strlen(text));
memcpy(tmp,text,strlen(text));
int i=0;
while(tmp[i] != '\0') {
if ( isalpha(tmp[i])) {
if ( isupper(tmp[i]) ) {
tmp[i]=(tmp[i] - 'A' + KEY)%26 + 'A';
} else {
tmp[i]=(tmp[i] - 'a' + KEY)%26 + 'a';
}
}
i++;
}
printf("Cypher: %s\n",tmp);
free(tmp);
return 0;
}
posted on 2011-01-17 23:23 志華 閱讀(490) 評(píng)論(0) 編輯 收藏 引用 所屬分類: C/C++

