غريبة غير مرغوب فيها من ثلاثة أرقام من المطبوعات من Caesar Cipher
سؤال
رمز الشفرات يعمل في الواقع ؛ إنه فقط أحصل على بعض الرموز الفردية المكونة من ثلاثة أرقام مفصولة بالرقابات أيضًا. أي مساعدة سيكون موضع تقدير كبير. هذا هو الكود الخاص بي.
تبدو الرموز هكذا ولكن لها أرقام عشوائية/354/333/645/810/236
#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;
}
};
المحلول
أنت لا تنهي encrypted
سلسلة بشكل صحيح.
انت تحتاج:
- للتأكد
char encrypted[len + 1];
. encrypted[len] = '\0';
بعد الحلقة.
لا تنتمي إلى StackOverflow