2017-03-04 106 views
0

我的一段代碼片段。我的下面的功能接受來自用戶的文本。反轉整個輸入,然後嘗試將反轉字符串的ASCII表示存儲在名爲ascii_array的數組中。但是,如代碼所示,它只接受反轉字符串的第一個字符,並增量並覆蓋ascii_array的第一個位置。我嘗試了幾種方法來增加指針來存儲ascii_array中的字符,但是出現錯誤。該數組應包含ASCII數組中反轉字符串的ASCII表示。C:指針和數組

例如: 輸入:你好!反向字符串

ASCII表示:elloh

預計產量: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

在括號後面加上圓括號 –

+0

是不是'* p ++ = temporary;'而不是'* p = temporary ++;'您正在尋找的所有內容? – Dolda2000

回答

2

我想有一個整數數組。因爲在後面的代碼中,我將爲這些整數添加值。

如果要將char *轉換爲整數數組,則不需要做太多工作。 A 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

在未來,你應該嘗試用更少的線來做到這一點:)