int main(int argc, string argv[]){
// variable declarations
if (argc != 2){
printf("No key value was input");
return 1;
}
string key = "";
int i = 0;
int j = 0;
int q = 0;
int keylen = 0;
string plaintext = "";
int plainlen = 0;
key = argv[1];
keylen = strlen(key);
for(i = 0; i <keylen; i++){
//convert all key to lower case
if((key[i] < 65 || key[i] > 122) || (key[i] > 90 && key[i] < 97)){
printf("None alphabetical character detected. Exiting...");
return 1; }
key[i] = tolower(key[i]);
key[i] = key[i]-97;
}
printf("Plaintext: ");
plaintext = get_string();
plainlen = strlen(plaintext);
printf("ciphertext: ");
for(i = 0; i < plainlen; i++){
if(q > plainlen){
break;
}
// outer text loop
for(j = 0; j < keylen; j++){
if(q > plainlen){
break;
}
// inner text loop
// if we have non-alphabetic
if((plaintext[q] < 65 || plaintext[q] > 122) || (plaintext[q] > 90 && plaintext[q] < 97)){
printf("%c", plaintext[q]);
//can we go negative here???
j--;
q++;
}
// check if it's wrapped
else if(((plaintext[q]+(key[j]) > 90 && toupper(plaintext[q]) == plaintext[q])) || ((plaintext[q]+(key[j]) > 122 && tolower(plaintext[q]) == plaintext[q]))){
printf("%c", (plaintext[q]+key[j]-26));
q++;
}
// normal shift
else{
printf("%c", (plaintext[q]+key[j]));
q++;
}
}
}
printf("\n");
return 0;
}
答案 0 :(得分:0)
我认为这行有printf("Plaintext: ");
它应该是printf("plaintext: ");
请注意,要打印的文本的第一个字母不是大写。
正如您自己提到的那样,您的输出是正确的,这是唯一可能的错误。下次,彻底检查问题的规格。