0
密碼實際工作;這只是我得到一些奇怪的三位數字代碼與斜槓分開。 任何幫助將不勝感激。這是我的代碼。從凱撒密碼奇怪的不需要的三位數碼打印輸出
的代碼看起來是這樣,但有隨機數/ 354 /645分之233/236分之810
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cs50.h"
int i, len;
string sentance, encrypted;
int k, argvLen;
int caesar (int k){
printf("Hi I'm Ceaser! What would you like me to cipher?\n");
sentance = GetString();
len = strlen(sentance);
char encrypted[len];
for (i=0; i<len; i++) {
if (sentance[i] >='a' && sentance[i] <='z') {
encrypted[i] = ((sentance[i] - 'a' + k) % 26) + 'a';
}
else if (sentance[i] >='A' && sentance[i] <='Z') {
encrypted[i] = ((sentance[i] - 'A' + k) % 26) + 'A';
}
else if (sentance[i] >=' ' && sentance[i] <= '@'){
encrypted[i] = sentance[i];
}
}
printf("%s", encrypted);
return 0;
};
int main (int argc, const char * argv[]) {
if (argc==2) {
k = atoi(argv[1]);
argvLen = strlen(argv[1]);
for (i=0; i<argvLen; i++){
if (isdigit(argv[1][i])){
caesar(k);
}
else {
printf("please enter a number for the key!");
return 1;
}
}
return 0;
}
};
非常感謝您的意見,現在它終於有效!我理解了爲終止字符留下空間的一點,但是第二部分做的是終止字符的'\ 0'魔術,然後在從0開始的字符串中使用[len]放置在字符串的末尾。右? – joewoodward 2010-10-03 01:46:00