2012-11-26 7 views
0

다음 질문이 있습니다. 나는 그것을 암호화하기 위해 사용자 입력과 사용자 캐서 사이퍼를 읽을 필요가있다. 그러나 사용자 입력을 읽는 동안 다음과 같은 문제가 생겼습니다. 예를 들어 "./caesar 3 저는" 입니다. 문제가 문자 인 것 같습니다. ' 이 프로그램은 다른 입력에도 사용할 수 있습니다.C : " '"을 포함한 사용자 입력 문자열 - 문자

/** 
* 
* 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; 
    } 

답변

5

쉘은 '을 문자열의 시작으로 해석합니다.

./caesar 3 I\'m

또는 큰 따옴표로 인수를 묶 : 그래서 당신은 그것을 탈출하거나 필요한이이 프로그램과 아무 상관이

./caesar 3 "I'm"

하는 것으로. 이것을 처리하는 명령 행 쉘만입니다.

+0

감사합니다. – siebenschlaefer