我已经解决了验证问题,但是现在我只得到了一个字符串作为密文,却找不到问题。我可能只是想念一点点,但任何帮助都值得赞赏。 顶部是我的终端窗口,底部是我的代码。
~/caesar/ $ make caesar
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow caesar.c -lcrypt -lcs50 -lm -o caesar
~/caesar/ $ ./caesar 12
plaintext: "world, say hello!"
ciphertext: aaaaaaaaaaaaaaaaaaa
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(int argc, string argv[])
{
if(argc == 2)
{
int n = strlen(argv[1]);
int i = 0;
for(i = 0; i < n; i++)
{
if(!isdigit(argv[1][i]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
int key = atoi(argv[1]);
string text = get_string("plaintext: ");
printf("ciphertext: ");
int l = 0;
int t = strlen(text);
for(l = 0; l < t; l++)
{
if(isupper(text[i]))
{
printf("%c", (((text[i] - 'A') + key) % 26) + 'A');
}
else if(islower(text[i]))
{
printf("%c", (((text[i] - 'a') + key) % 26) + 'a');
}
else
{
printf("%c", text[i]);
}
}
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
printf("\n");
return 0;
}
答案 0 :(得分:1)
您一直在测试文本[i]而不增加i,您应该测试文本[l]
int main(int argc, string argv[])
{
if(argc == 2)
{
int n = strlen(argv[1]);
int i = 0;
for(int l = 0; l < n; l++)
{
if(!isdigit(argv[1][l]))
{
printf("Usage: ./caesar key\n");
return 1;
}
}
int key = atoi(argv[1]);
string text = get_string("plaintext: ");
printf("ciphertext: ");
int t = strlen(text);
for(int l = 0; l < t; l++)
{
if(isupper(text[l]))
{
printf("%c", (((text[l] - 'A') + key) % 26) + 'A');
}
else if(islower(text[l]))
{
printf("%c", (((text[l] - 'a') + key) % 26) + 'a');
}
else
{
printf("%c", text[l]);
}
}
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
printf("\n");
return 0;
}
此代码更干净。
答案 1 :(得分:0)
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, string argv[])
{
if (argc == 2)
{
int digits = strlen(argv[1]);
bool isnum = true;
for (int i = 0; i < digits; i++)
{
if (isdigit(argv[1][i]) == 0)
{
isnum = false;
break;
}
}//checks weather the give input of argument are correct
if (isnum == true)
{
string text = get_string("Enter the text: ");
char textalpha[25];
char ciphertext[strlen(text)];
int a = atoi(argv[1]); //converts argv to int
for (int y = 0; y <= 25 ; y++)
{
textalpha[y] = (char)(y + 97);
// creates an array of alphabets with index start from 0 to 25
}
//ciphertext[w] = (textalpha[w] +a) % 26; This the main forluma for rotating the alphabets
for (int w = 0; w <= strlen(text); w++)
{
if (isalpha(text[w]))
{
if (isupper(text[w]))
{
ciphertext[w] = textalpha[((((int)text[w] - 65) + a) % 26)];
//printf("%c",toupper(ciphertext[w]));
ciphertext[w] = toupper(ciphertext[w]);
}
else if (islower(text[w]))
{
ciphertext[w] = textalpha[((((int)text[w] - 97) + a) % 26)];
//printf("%c",tolower(ciphertext[w]));
ciphertext[w] = tolower(ciphertext[w]);
}
}
else
{
//printf("%c",text[w]);
ciphertext[w] = text[w];
}
}
printf("ciphertext: %s\n", ciphertext);//prints the cipher text
return 0;
}
else
{
printf("Usage: ./caesar key\n");
// prints if the entered arguments are not in this form
return 1;
}
}
else
{
printf("Please provide us with 'one' argument.\n");
// prints if the arguments entered are more or less than two.
return 1;
}
}