先贴
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;
}