WIKIPEDIA的
链接
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <ctype.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;
}
char *KEY = (char *) malloc(sizeof(char) * strlen(argv[1]));
strcpy(KEY,argv[1]);
printf("Plain Text:");
char *text=GetString();
char *tmp=(char *)malloc(sizeof(char) * strlen(text));
memcpy(tmp,text,strlen(text));
int KEN_LEN = strlen(KEY);
for(int i=0;tmp[i] != '\0';i++) {
static int j = 0;
if ( isalpha(tmp[i]) ) {
int key = toupper(KEY[j%KEN_LEN]) - 'A';
if ( isupper(tmp[i]) ) {
tmp[i]=(tmp[i] - 'A' + key)%26 + 'A';
} else {
tmp[i]=(tmp[i] - 'a' + key)%26 + 'a';
}
j++;
}
}
printf("Cipher: %s\n", tmp);
free(KEY);
free(tmp);
free(text);
return 0;
}