|
5鱼币
这段代码在argv[1]的位置应输入的是数字,但是每次运行的时候,比如./ caesar 2,就会显示Segmentation fault。豪无头绪,请大神帮忙!
- #include <cs50.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #include <ctype.h>
- int main(int argc, string argv[])
- {
- if (argc == 2 && isdigit(argv[1]))
- {
- int key = atoi(argv[1]);
- string plaintext = get_string("plaintext: ");
- printf("ciphertext: ");
- for (int i = 0, len = strlen(plaintext); i < len; i++)
- {
- char c = plaintext[i];
-
- if (isalpha(c))
- {
- if (islower(c))
- {
- printf("%c", (((c - 97) + key) % 26) + 97);
-
- }
- if (isupper(c))
- {
- printf("%c", (((c - 65) + key) % 26) + 65);
-
- }
- else
- {
- printf("%c", c);
-
- }
- }
- }
- return 0;
- }
- else
- {
- printf("Usage: ./caesar key\n");
- return 1;
- }
- }
复制代码 |
|