我需要在用户输入和用户凯撒暗号进行加密阅读。不过,虽然在读我得到以下问题的用户输入,如果我例如进入我的程序不会终止:“./caesar 3 I'm
”这个问题似乎是字符'
。该方案适用于其他输入。
/**
*
* caesar.c
*
* The program caesar encrypts a String entered by the user
* using the caesar cipher technique. The user has to enter
* a key as additional command line argument. After that the
* user is asked to enter the String he wants to be encrypted.
*
* Usage: ./caesar key [char]
*
*/
#include <cs50.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int caesarCipher(char original, int key);
int main(int argc, string argv[])
{
if (argc > 1)
{
int key = atoi(argv[1]);
for (int i = 2; i < argc; i++)
{
for (int j = 0; j < strlen(argv[i]); j++)
{
argv[i][j] = caesarCipher(argv[i][j], key);
}
}
for (int i = 2; i < argc; i++)
{
printf("%s", argv[i]);
}
return 0;
}
else
{
printf("The number of command arguments is wrong! \n");
return 1;
}
}
int caesarCipher(char original, int key)
{
char result = original;
if (islower(original))
{
result = (original - 97 + key) % 26 + 97;
}
else if (isupper(original))
{
result = (original - 65 + key) % 26 + 65;
}
return result;
}
外壳解释'
作为一个字符串的开始。所以,你需要或者逃避它:
./caesar 3 I\'m
或用双引号的自变量:
./caesar 3 "I'm"
请注意,这已经无关,与你的程序。这是唯一的命令行shell与此交易。