此功能中有什么错误? (初学者CS50)
我正在尝试使用此功能将给定文本(字符串A)用给定的字母(字符串B)加以关注。 它编译和运行,实际上替换字母不是正确的方式。
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
string replace(string a , string b);
int main(int argc, string argv[])
{
string code;
if(argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
else
{
code = argv[1];
if(strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
for(int i = 0; i < 26; i++)
{
if (isalpha(code[i]) == 0)
{
printf("Key must only contain alphabetic characters.\n");
return 1;
}
}
for(int j = 0; j < 26; j++)
{
for(int n = j + 1; n < 26; n++)
{
if(code[j] == code[n])
{
printf("Key must not contain repeated characters.\n");
return 1;
}
}
}
}
string txt = get_string("plaintext:");
printf("ciphertext: %s\n" , replace(txt , code));
return 1;
}
string replace(string a , string b)
{
string final = a;
string alpha = {"abcdefghijklmnopqrstuvwxyz"};
for (int i = 0; i < strlen(a); i++)
{
for(int n = 0; n < 26; n++)
{
if(a[i] == alpha[n])
{
final[i] = b[n];
}
}
}
return final;
}
我只想了解这是我的一些示例:
./substitution ytnshkvefxrbauqzclwdmipgjo
plaintext: hello
ciphertext hhbbq
当我再尝试一次,输出为ehbbq
时,这是正确的,但是每次我尝试时,它都不一样。
i'm trying to cipher a given text (string a) with a given set of letters (string b) using this function.
it compiles and runs and actually replaces the letters just not the right way.
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
string replace(string a , string b);
int main(int argc, string argv[])
{
string code;
if(argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
else
{
code = argv[1];
if(strlen(argv[1]) != 26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
for(int i = 0; i < 26; i++)
{
if (isalpha(code[i]) == 0)
{
printf("Key must only contain alphabetic characters.\n");
return 1;
}
}
for(int j = 0; j < 26; j++)
{
for(int n = j + 1; n < 26; n++)
{
if(code[j] == code[n])
{
printf("Key must not contain repeated characters.\n");
return 1;
}
}
}
}
string txt = get_string("plaintext:");
printf("ciphertext: %s\n" , replace(txt , code));
return 1;
}
string replace(string a , string b)
{
string final = a;
string alpha = {"abcdefghijklmnopqrstuvwxyz"};
for (int i = 0; i < strlen(a); i++)
{
for(int n = 0; n < 26; n++)
{
if(a[i] == alpha[n])
{
final[i] = b[n];
}
}
}
return final;
}
i just want to understand whats up with it here are some of my samples:
./substitution ytnshkvefxrbauqzclwdmipgjo
plaintext: hello
ciphertext hhbbq
and when i try it one more time that output is ehbbq
which is the right one but every time i try it it's different.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论