2014-03-05 11 views
0

저는 C 언어에 익숙하지 않습니다. 기본 암호를 만들기 위해 'x'번 알파벳 문자를 옮기고 싶습니다.C 시저 암호 ASCII 알파벳 랩

islower() 함수에 문제가 있습니다. 나는 'i'를 사용하고 있지만, 캐릭터로 바꿀 수는 없습니다.

#include <cs50.h> 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <ctype.h> 

string p; 

int main(int argc, string argv[]) 
{ 
    //if argument count does not equal 2, exit and return 1 
    if (argc != 2) 
    { 
     printf("Less or more than 2 arguments given, exiting...\n"); 
     return 1; 
    } 
    else //prompt user for plaintext to encrypt 
    { 
     p = GetString(); 
    } 

    //take the second part of the array (the int entered by user) and store as k (used as the encryption key) 
    //string k = argv[1]; 
    int k = atoi(argv[1]); 

    //function: 
    // c = (p + k) % 26;  
    //iterate over the characters in the string 
    //p represents the position in the alphabet of a plaintext letter 
    //c likewise represents a position in the alphabet 
    char new; 
    for (int i = 0, n = strlen(p); i < n; i++) 
    if (islower((char)i)) 
    { 
     //printf("%c\n", p[i] + (k % 26)); 
     printf("This prints p:%s\n", p); 
     printf("This prints i:%d\n", (char)i); 
     printf("This prints k:%d\n", k); 
     printf("This prints output of lower(i):%d\n", islower(i)); 
     new = (p[i] - 97); 
     new += k; 
     //printf("%d\n", new %26 + 97); 
     //printf("i = |%c| is lowercase\n", i); 
     printf("%c\n", new % 26 + 97); 
    } 
    else { 
     //printf("%c", p[i] + (k % 26)); 
     printf("This prints p:%s\n", p);  
     printf("This prints i:%d\n", (char)i); 
     printf("This prints k:%d\n", k); 
     printf("This prints output of lower(i):%d\n", islower(i)); 
     new = (p[i] - 65); 
     new += k; 
     //printf("%d\n", new % 26 + 65); 
     //printf("i = |%c| is uppercase\n", i); 
     printf("%c\n", new % 26 + 65); 
    } 
    printf("\n"); 
} 

출력 : ASCII에 정의 된 영어

[email protected] (~/Dropbox/CS50x/pset2): ./caesar2 1 
zZ < here is my input 
This prints p:zZ 
This prints i:0 
This prints k:1 
This prints output of lower(i):0 
G < here is fails, lower case z should move to lower case a 
This prints p:zZ 
This prints i:1 
This prints k:1 
This prints output of lower(i):0 
A < here is a success! upper case Z moves to upper case A 
+1

모듈러스 연산자'%'는'+'보다 우선 순위가 높습니다. 나는 당신이라면'printf()'에서 괄호를 사용할 것이다. –

+0

감사합니다. 고맙습니다. – JT1

+0

reza는 (p [i] + k) % 26을 대신 사용한다고 생각합니다. – user1895961

답변

2

알파벳 C로 사용된다. 'Z'(ASCII 90) 다음에 '{'만 (ASCII 91)이옵니다. 는 다음과 같은 방식으로 모든 변화를해야한다, 다시 'A'로 이동 :

  1. 은 65하여 ASCII 문자를 뺀다은 0 25 (포함) 사이의 출력을 초래할 수 있습니다.
  2. 위치 변경 (시프트 거리)을 추가하십시오.
  3. 귀하의 결과를 감안하여 26을 모듈로 취하십시오.
  4. 65를 다시 추가하십시오.

영어의 대문자 만 사용할 수 있습니다. 따라서 ctype.h 라이브러리의 toupper()을 사용할 수 있습니다.

작은 문자에 대해서도 비슷한 기능을 추가하려면 65를 97로 바꾸어 위의 절차를 수행하십시오. 작은 문자 또는 대문자인지 확인하려면 isupper()을 사용하십시오. 특수 문자에 대해 특정 코드를 추가해야합니다.

+0

isupper() 및 islower()가 내가 찾고있는 것입니다. – JT1

+0

1 단계에서 int new = p - 65 그러나 출력은 문자열입니다. int로 유지해야하지 않습니까? – JT1

+0

원하는대로 할 수 있습니다. 나는'char'을 추천 할 것이지만, 더 적은 메모리가 필요하다. –

0

islower((char)i)은 루프 카운터가 소문자인지 확인합니다.

해당 위치의 문자 - islower(p[i])을 테스트하고 싶습니다.