C:用户输入字符串,包括'&引用-性格

C:用户输入字符串,包括'&引用-性格,c,encryption,char,caesar-cipher,cs50,C,Encryption,Char,Caesar Cipher,Cs50,我需要读入用户输入和用户Caesar cypher来加密它。但在读取用户输入时,我遇到了以下问题:如果我输入例如:“/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 k

我需要读入用户输入和用户Caesar cypher来加密它。但在读取用户输入时,我遇到了以下问题:如果我输入例如:“
/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程序对用户输入的字符串进行加密
*使用凯撒密码技术。用户必须输入
*作为附加命令行参数的键。在那之后
*要求用户输入要加密的字符串。
*
*用法:./caesar键[char]
*
*/
#包括
#包括
#包括
#包括
#包括
int密码(原始字符,int密钥);
int main(int argc,字符串argv[])
{
如果(argc>1)
{    
int key=atoi(argv[1]);
对于(int i=2;i
shell将
'
解释为字符串的开头。所以你要么逃避它:

./caesar 3 I\'m /凯撒3我是 或者用双引号将参数括起来:

./caesar 3 "I'm" /凯撒3“我是”
请注意,这与您的程序无关。只有命令行shell可以处理这个问题。

shell将
'
解释为字符串的开头。所以你要么逃避它:

./caesar 3 I\'m /凯撒3我是 或者用双引号将参数括起来:

./caesar 3 "I'm" /凯撒3“我是” 请注意,这与您的程序无关。只有命令行shell处理这个问题