2012-11-22 5 views
2

C에서 rot13-algorithm을 구현하려고합니다. 하지만 그 언어에 익숙하지 않아서 여기에 제 코드에 몇 가지 문제가 있습니다.Rot13-Implementation 정보

기본적으로 args []의 모든 문자를 13 위치 위로 회전하고 싶습니다. 는하지만이 코드는 매우 부진 할 것으로 보인다 :

#include <stdio.h> 

char[] rotate(char c[]) { 
    char single; 
    int i; 
    int alen = sizeof(c)/sizeof(c[0]); 
    char out[alen]; 

    for(i=0;i<=alen;i+=1) { 
    if(c[i]>='a' && (c[i]+13)<='z'){ 
     out[i] = c[i]+13; 
    } 
    } 

    return out; 
} 

int main(int argc, char *argv[]) { 
    printf("The given args will be rotated\n"); 
    int i; 
    char rotated[sizeof(argv)/sizeof(argv[0])]; 

    rotated = rotate(argv); 

    /* printing rotated[] later on */ 
    return 0; 

} 

나는 구멍이 많이 여기가 알 - 당신은 어떻게이 문제를 해결하는 저를 보여줄 수?

+3

당신이 도움 enlightend 받고 코드를 단계별로 (당신이 리눅스에 있다면'gdb' 같은) 디버거를 사용하여 배우고 싶은 수 있습니다. – alk

+0

'gcc -Wall -Werror -pedantic -std = c99' (리눅스에서) 코드를 먼저 컴파일 해보십시오. –

+1

결코 끝나지 않을 것입니다. . 함수는 배열을 반환 할 수 없으므로'char [] rotate (char c []) {/ * ... * /}'는 컴파일되지 않습니다. 또한'rotate = rotate (argv);'와 같이 배열 전체에 할당 할 수 없습니다. 어쩌면 당신은 당신의 문제에 대해 더 구체적 일 수 있습니다. –

답변

1

C의 배열 크기는 컴파일시 설정해야하므로 배열 크기에 대해 상수가 아닌 식을 사용할 수 없습니다.

// in place rotate 
void rotate(char *str) 
// str must be a zero-terminated string 
{ 
    int i =0; 
    // loop until str itself is not NULL and str[i] is not zero 
    for(i=0;str && str[i]; ++i) // ++i is a pre-increment 
    { 
    if(str[i] >= 'a' && (str[i]+13) <='z') 
    { 
     str[i] = str[i]+13;  // modifying str in place 
    } 
    } 
} 

이 그런 다음 main()은 다음과 같이 할 수 있습니다 :

는 아래의 구현을 고려하는 것이이 사건을 담당 변환의

int main(int argc, char *argv[]) 
{ 
    printf("The given args will be rotated: %s\n", argv[1]); 

    rotate(argv[1]); 

    printf("Rotated: %s\n", argv[1]); 
    return 0; 
} 

업데이트보다 진보 된 버전을 때 str[i] + 13 > 'z'

for(i=0;str && str[i]; ++i) // ++i is a pre-increment 
    { 
     // ignore out of range chars 
     if (str[i] < 'a' || str[i] > 'z') continue; 
     // rotate 
     for (off = 13; off > ('z' - str[i]);) 
     { 
      off-= (1 + 'z' - str[i]); 
      str[i] = 'a'; 
     } 
     str[i]+=off;  
    } 
+0

정교하게 주시겠습니까? 안토니 Mathys 가리키는 것처럼 –

+0

그리고 내 대답을 지적한대로 변환 올바르지 않습니다. 그러나 'i = 0; str && str [i]; ++ i' –

+0

ROT (N) –

2

@Michael은 정수가 아닌 값으로 배열 크기를 선언 할 수 없기 때문에이 char out[alen]을 컴파일러에서 허용하지 않는다고 말했습니다. 코드의 또 다른 문제점은 for 루프 for(i = 0; i < = alen; i+=1) 어레이가 0에서 시작하기 때문에 lenght's 위치까지를 수행하면 배열에서 벗어납니다. 코드에 대해

: 당신은 C에서 배열을 반환 할 수 없습니다 (하지만 당신은 포인터를 반환 할 수 있습니다) 때문에

  1. 당신은, 함수의 인수로 문자열의 시작에 대한 포인터를 사용해야합니다.
  2. 일부 문자를 기호로 변환하기 때문에 if(str[i] >= 'a' && (str[i]+13) <='z')이 올바르지 않습니다.

________ enter image description here -------------------------- ASCII CODE!

void rotate(char * str) 
    { 
     int i = 0; 

     /* You do this until you find a '\0' */ 
     for(i = 0; str[ i ] != '\0' ; i++){ 

      /* Use the pointer notation if you passed a pointer. */ 
      /* If the letter is between a and m you can simply sum it. */ 
      if(*(str + i) >= 'a' && *(str + i) < 'n') 
       *(str + i) += 13;  

      /* If the letter is between the n and z you have to do the opposite.*/ 
      else if(*(str + i) >= 'n' && *(str + i) <= 'z') 
       *(str + i) -= 13; 
     } 
    } 
4

고마워 얘들 아, 내가 ROT13 문자열에서에 /이 코드

#include <stdio.h> 

int rot13(int c){ 
    if('a' <= c && c <= 'z'){ 
    return rot13b(c,'a'); 
    } else if ('A' <= c && c <= 'Z') { 
    return rot13b(c, 'A'); 
    } else { 
    return c; 
    } 
} 

int rot13b(int c, int basis){ 
    c = (((c-basis)+13)%26)+basis; 
    return c; 
} 

int main() { 
    printf("The given args will be rotated"); 
    int c; 
    while((c = getchar()) != EOF){ 
    c = rot13(c); 
    putchar(c); 
    } 
    return 0; 
} 
1

이 기능은 인코딩 할 수 있습니다/디코딩으로 문제를 해결했다. VIM의 g? rot13 인코더와 호환됩니다.

void rot13 (char *s) { 
    if (s == NULL) 
     return; 

    int i; 
    for (i = 0; s[i]; i++) { 
     if (s[i] >= 'a' && s[i] <= 'm') { s[i] += 13; continue; } 
     if (s[i] >= 'A' && s[i] <= 'M') { s[i] += 13; continue; } 
     if (s[i] >= 'n' && s[i] <= 'z') { s[i] -= 13; continue; } 
     if (s[i] >= 'N' && s[i] <= 'Z') { s[i] -= 13; continue; } 
    } 
}