2017-03-04 12 views
0

私のコードを抜粋したスニペット。以下の私の関数は、ユーザーからのテキストを受け入れます。入力全体を逆順にして、反転した文字列のASCII表現をascii_arrayという配列に格納しようとします。しかし、コードが反転した文字列の最初の文字を受け入れるだけなので、ascii_arrayの最初の位置をインクリメントして上書きします。私はascii_arrayに文字を格納するポインタを増やすいくつかの方法を試しましたが、エラーが発生します。配列には、ASCII配列の反転文字列のASCII表現が含まれている必要があります。C:ポインタと配列

例: 入力:hello!

逆に、文字列のASCII表現:!33 111 108 108 101 104

私が手出力:33

#include<string.h> 
#include<stdio.h> 

void encrypt(char inputText[], int inputLength); 

int main(void) 
{ 
    char word[512]; 
    int length; 
    printf("Enter word: "); 
    fgets(word,512,stdin); 
    length = strlen(word); 
    encrypt(word,length); 
    return 0; 

} 

void encrypt(char inputText[], int inputLength) 
{ 

    printf("%s\n",inputText); 
    char *begin = inputText; 
    char *end = inputText + inputLength - 1; 
    char temp; 
    while(end > begin) 
    { 
     temp = *end; 
     *end-- = *begin; 
     *begin++ = temp; 

    } 

    char *ascii_pointer; 
    char temporary; 
    ascii_pointer = inputText + 1; 
    temporary = *ascii_pointer; 

    int *p; 
    int array[512]; 
    p = array; 
    while(ascii_pointer < inputText + inputLength) 
    { 
     printf("INPUTTEXT: ascii_pointer: %s\n " , ascii_pointer); 
     temporary = *ascii_pointer++; 
     printf("temporary: %c\n " , temporary); 
     *p = temporary++; 
     // I think my logic is incorrect fron this point. 
     //*p++ = temporary++; 
     //*p++; 
     printf("asci_pointer: %c\n " ,*p); 
     printf("ascii_array: %d\n ", *array); 
     printf("\n"); 
    } 

    printf("\n"); 
    printf("END: %p:%c\n", p, *p); 
    printf("END--- MAYBE GARBAGE VALUE: place holder: %c\n ",temporary); 
    printf("END: ascii_array: %d\n ", *array); 
    return; 

} 
+0

[mcve]を入力してください。また、正確な入力、期待される出力、実際の出力を提供する。 – kaylum

+0

post pp pp単位の前後に括弧を入れてください –

+0

'* p = temporary ++;ではなく、' * p ++ = temporary; 'ではありませんか? – Dolda2000

答えて

2

私が持っていると思います

予想される出力をelloh整数の配列後でコードで私はそれらの整数に値を追加するので。

char *を整数の配列に変換する場合は、あまり多くの作業がありません。 charはすでに1バイトの整数です。あなたはそのままそれをそのまま残し、数学をすることができます。

例えば

...

int main(void) { 
    char string[] = "Hello"; 

    // Iterate through the string using the pointer until we see null. 
    // It avoids having to use strlen() which scans the whole string. 
    for(char *tmp = string; tmp[0] != '\0'; tmp++) { 
     // Print the char as an integer. 
     printf("%d\n", tmp[0]); 
    } 
} 

$ ./test 
72 
101 
108 
108 
111 

あなたはこれらの数字を操作したい場合は、先に行きます!各文字に2を加えた例を次に示します。

int main(void) { 
    char string[] = "Hello"; 

    // Iterate through the string using the pointer until we see null. 
    // It avoids having to use strlen() which scans the whole string. 
    for(char *tmp = string; tmp[0] != '\0'; tmp++) { 
     tmp[0] += 2; 
    } 

    puts(string); 
} 

$ ./test 
Jgnnq 

だけcharは、だからあなたの暗号化機能を行うために必要なすべての文字列を逆のシングルバイトであり、唯一の127

まで保存できることに注意してください。あなたが本当には、整数の配列に文字列を変換したい場合は

void encrypt(char inputText[], int inputLength) 
{ 
    printf("%s\n",inputText); 
    char *begin = inputText; 
    char *end = inputText + inputLength - 1; 
    char temp; 
    while(end > begin) 
    { 
     temp = *end; 
     *end-- = *begin; 
     *begin++ = temp; 

    } 
} 

は、単純なループを行います。

int main(void) { 
    char string[] = "Hello"; 

    // Allocate an integer array to hold each integer in the string. 
    // This ignores the null byte. 
    int *copy = malloc(strlen(string) * sizeof(int)); 

    size_t len = strlen(string); 

    // Simple iteration to copy the string. A char will always 
    // be smaller than an int so the assignment just works. 
    for(size_t i = 0; i < len; i++) { 
     copy[i] = string[i]; 
    } 

    // Print out each integer in the new array. 
    for(size_t i = 0; i < len; i++) { 
     printf("%d\n", copy[i]); 
    } 

    free(copy); 
} 
1

あなたはこのコードを試してみて、あなたはいくつかのエラー(コメントを参照)やったかを理解する必要があります "!こんにちは"

void encrypt(char inputText[], int inputLength) 
{ 
    printf("%s\n",inputText); 
    char *begin = inputText; 
    char *end = inputText + inputLength - 1; 
    char temp; 
    while(end > begin) 
     { 
     temp = *end; 
     *end-- = *begin; 
     *begin++ = temp; 
     } 

    char *ascii_pointer; 
    char temporary; 
    // ascii_pointer = inputText + 1; => you miss the first character 
    ascii_pointer = inputText; 
    temporary = *ascii_pointer; 

    int *p; 
    int array[512]; 
    p = array; 
    while(ascii_pointer < inputText + inputLength) 
     { 
     printf("INPUTTEXT: ascii_pointer: %s\n " , ascii_pointer); 
     temporary = *ascii_pointer++; 
     printf("temporary: %c\n " , temporary); 
     // *p = temporary++; => you always write at the first array position 
     *p++ = temporary++; 
     printf("ascii_array: %d\n ", *array); 
     printf("\n"); 
     } 
    printf("\n"); 
    printf("END: %p:%c\n", p, *p); 
    printf("END--- MAYBE GARBAGE VALUE: place holder: %c\n ",temporary); 
    // printf("END: ascii_array: %d\n ", *array); => you print only the first array element 
    printf("END: ascii_array: "); 
    p = array; 
    while (p < array + inputLength) 
     printf("%d ", *p++); 
    printf("\n"); 
    return; 
} 

のためにあなたを与える:

END: ascii_array: 33 111 108 108 101 104

を将来、たくさんの行でこれをやろうとしてください:)