CS50-pset2-替换 [英] CS50 - pset2 - substitution
本文介绍了CS50-pset2-替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我看不到pset2替换的代码丢失了什么。当我使用check 50测试程序时,它返回以下结果:
:)substitution.c存在
:)substitution.c编译
:(使用ZYXWVUTSRQPONMLKJIHGFEDCBA作为密钥将"A"加密为"Z" 需要"密文:Z...",而不是"密文:Z..." :(使用ZYXWVUTSRQPONMLKJIHGFEDCBA作为密钥将"a"加密为"z" 需要"密文:Z...",而不是"密文:Z..." :(使用NJQSUYBRXMOPFTHZVAWCGILKED作为密钥将"abc"加密为"njq" 需要"密文:nj...",而不是"密文:nj..." :(使用NJQSUYBRXMOPFTHZVAWCGILKED作为密钥将"XYZ"加密为"KED" 需要"密文:KE...",而不是"密文:KE..." :(使用YUKFRNLBAVMWZTEOGXHCIPJSQD作为密钥将"This is CS50"加密为"Cbah ah KH50" 需要"密文:cb...",而不是"密文:cb..." :(使用yukfrnlbavmwzteogxhcipjsqd将"This is CS50"加密为"Cbah ah KH50" 需要"密文:cb...",而不是"密文:cb...":(使用YUKFRNLBAVMWZteogxhcipjsqd将"This is CS50"加密为"Cbah ah KH50" 需要"密文:cb...",而不是"密文:cb..."
:(使用DWUSXNPQKEGCZFJBTLYROHIAVM作为密钥加密所有字母字符 需要"密文:RQ...",而不是"密文:RQ...":)处理密钥不足
:)处理无效的密钥长度
:)处理密钥中的无效字符
:)处理密钥中的重复字符
:)处理键中的多个重复字符
但是,当我手动键入密钥和明文时,它的工作方式与预期完全相同。此外,检查50的"预期"结果似乎与输出完全相同,因此错误之处并不明显。我的代码如下:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int get_validkey(string A);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("useage: ./substitution key
");
return 1;
}
int validation = get_validkey(argv[1]);
if (validation == 1)
{
printf("key must contain 26 alphabetical characters
");
return 1;
}
// prompting user for plaintext
string plaintext = get_string("plaintext: ");
printf("ciphertext: ");
int length = strlen(plaintext);
for (int c = 0; c <= length ; c++)
{
// printing any non-alphabet characters unchanged
if (plaintext[c] < 'A' || (plaintext[c] > 'Z' && plaintext[c] < 'a') || plaintext[c] > 'z')
{
printf("%c", plaintext[c]);
}
else
{
for (int b = 0; b <= 25; b++)
{
if (plaintext[c] == 65 + b)
{
char upper = argv[1][b];
int up = isupper(upper);
if (up == 0)
{
upper = toupper(upper);
printf("%c", upper);
}
if (up != 0)
{
printf("%c", upper);
}
}
else if (plaintext[c] == 97 + b)
{
char lower = argv[1][b];
int low = islower(lower);
if (low == 0)
{
lower = tolower(lower);
printf("%c", lower);
}
if (low != 0)
{
printf("%c", lower);
}
}
}
}
}
printf("
");
return 0;
}
// function assesses if the key input is valid and returns 0 if it is and 1 if it is not
int get_validkey(string A)
{
int inputlength = strlen(A);
if (inputlength != 26)
{
return 1;
}
else
{
for (int g = 0; g < 26; g++)
{
// checks if the character is non alphabetical
if (A[g] < 'A' || (A[g] > 'Z' && A[g] < 'a') || A[g] > 'z')
{
return 1;
}
// scans all characters before A[g] to see if it has already been used
for (int k = (g - 1); k >= 0; k--)
{
if (A[k] == A[g])
{
return 1;
}
// also checks if different case of the same character has been used
if (A[k] == A[g] + 32)
{
return 1;
}
if (A[k] == A[g] - 32)
{
return 1;
}
}
// scans all characters after A[g] to check if it has been used already. (Not sure if this is necessary)
for (int l = (g + 1); l < 26; l++)
{
if (A[l] == A[g])
{
return 1;
}
// also checks if a different case of the same letter is used
if (A[l] == A[g] + 32)
{
return 1;
}
if (A[l] == A[g] - 32)
{
return 1;
}
}
}
return 0;
}
}
我对编程非常陌生,任何帮助都将不胜感激。
推荐答案
我也遇到过同样的问题,但后来我意识到,当我为纯文本(For(int c=0;c<;=Long;c++))迭代For循环时,编程语言本身在计算时从0开始。即使我们知道它应该等于纯文本的长度,语言也会迭代到字符串数组中的那个空元素。我建议您这样做,因为(int c=0;c<;=长度;c++)。希望,这很有帮助。
这篇关于CS50-pset2-替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文