【发布时间】:2020-11-05 22:03:23
【问题描述】:
我正在尝试创建一个接受命令行参数来加密明文的程序! 程序在制作时必须在其名称后接受一个 cmd 行参数,这将是由纯文本(仅)字母字符由该键旋转的键(例如,它的数字被添加到真实的字母 ASCII 数字中,导致另一个字母打印出来!
它应该在存在一个参数时打印一条错误消息(例如这里:/make encipher) 而不是在这里:/make encipher 12
在没有 key 参数的情况下运行程序时出现分段错误,为什么?
这是完整的代码。我发布它是因为我需要了解我的错误的确切位置在哪里 为什么会触发?!
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h> // To use atoi (converting a string to an int)
#include <ctype.h>
#include <string.h>
bool key_is_numb(string argv[]);
void encipher(string txt, int key);
int main(int argc, string argv[])
{
if (key_is_numb(argv) == false)
{
printf("Usage: ./caesar key\n");
return 1;
}
else
{
int key = atoi(argv[1]);
string plaintext;
if (argc == 2 && key > 0)
{
plaintext = get_string("plaintext: ");
encipher(plaintext, key); // A function that prints the ciphered text
return 0; // returns Zero as main return value which means "All good"!
}
else if (argc == 1 || argc > 2 || key <= 0)
{
printf("Usage: ./caesar key\n");
return 1;
}
} // End else.
} // End main()å func.
bool key_is_numb(string argv[])
{
int n = strlen(argv[1]);
for (int i = 0; i < n; i++) // checking element by element in the second string of the argv[] array of strings
{
if (isdigit(argv[1][i]) == 0) // if the entered string "key" contains chars other than digits.
{
return false; // break out of the if statement & the entire function key_is_numb()
// and return false as soon as a letter is encountered.
}
else
{
continue; // go up & start the next iteration for the for loop.
}
// if only digits encountered then this for loop will come to an end and exist from here.
} // End for loop
return true; // function exits and return boolean true from here.
} // End key_is_numb() func.
void encipher(string txt, int key)
{
printf("ciphertext: ");
for (int i = 0, n = strlen(txt); i <= n; i++) // strlen counts the number of elements in a string excluding '\0'
{
char c = txt[i];
if (isalpha(c))
{
if (isupper(c))
{
char m = 'A'; // This is a modifyer character equals to 'A' = 65 so that it is indexed @ ZERO!
printf("%c", (c - m + key) % 26 + m );
//c = ((((int)txt[i] - 65) + key) % 26) + 65; // char c = 65 <-- 65 is an ASCII code equals 'A'
}
else if (islower(c))
{
char m = 'a'; // This is a modifying character 'a' = 97
printf("%c", (c - m + key) % 26 + m );
}
}// End if(alpha).
else
{
printf("%c", c);
}
} // End for().
printf("\n");
} // End encipher() func.
【问题讨论】:
-
你的代码假设总是有一个
argv[1]。您应该检查argc,它告诉您参数的数量。 -
我发布它是因为我需要了解我的错误的确切位置在哪里以及为什么会触发它?! -- 这就是调试器的目的,以及为什么它是学习如何使用的重要工具。
-
@PaulOgilvie 请在答案部分发布您的答案。谢谢。
-
string是char*的类型别名吗?如果是这样,为什么会做一些令人困惑的事情? -
@TedLyngmo
string是char *的cs50.htypedef 别名。它属于cs50.h
标签: c segmentation-fault cs50