簡體   English   中英

使用三個指針反轉字符串中的單詞?

[英]Reversing words in a string using three pointers?

我正在嘗試反轉字符串中的單詞,並且我相信自己編寫了正確的邏輯,但是在調試時我才知道我放入new_array char指針變量中的值丟失了嗎? 而且我似乎不知道為什么?

您能告訴我我正在做的錯誤是什么,可以做些什么來糾正它?

#include <stdio.h>

void reverse_words(char *arr, int size) {
    char *ptr = arr;
    char *new_array = (char*)malloc(sizeof(char*) * size);

    while (*ptr != '\0') {
        ptr++;
    }
    ptr--;

    for (int i = size - 1; i >= 0; i--) {
        if (*ptr != ' ') {
            ptr--;
        } else {
            char *temp = ptr;
            temp++;
            //
            // Problem is in this block new_array value is lost when i increment it                                
            while (*temp != ' ' && *temp != '\0') {
                *new_array = *temp;
                new_array++;
                temp++;
            }
            if (i != 0) {
                *new_array = *ptr;
                ptr--;
            }
        }
    }
    *new_array = '\0';
    strcpy(arr, new_array);
    return;
}

int main() {
    char arr[] = "My job is coding";
    int size = sizeof(arr);
    reverse_words(arr, size);
    printf("%s", arr);
    return 0;
}

您的代碼太復雜,有幾個問題:

  • 您分配了過多的內存: (char*)malloc(sizeof(char*) * size); 分配size以指針的大小。 使用malloc(size); 分配size字節。
  • 您的指針操作有錯誤,
  • 您忘記釋放分配的內存,從而導致內存泄漏。
  • 您不需要傳遞數組的大小,而是通過掃描空終止符來計算字符串的長度,只需為空終止符分配一個額外的字節。

還有一種替代解決方案,可以在不分配內存的情況下將字符串int中的單詞反轉:

  • 對於每個單詞,請反轉單詞
  • 最后一步:反轉字符串

這是使用實用程序功能的代碼:

#include <stdio.h>

void reverse_mem(char *str, int size) {
    for (int i = 0, j = size; i < --j; i++) {
        char c = str[i];
        str[i] = str[j];
        str[j] = c;
    }
}

void reverse_words(char *arr) {
     for (int i = 0, j = 0;; i = j) {
        for (; str[i] == ' '; i++)
            continue;
        if (str[i] == '\0')
            break;
        for (j = i; str[j] != '\0' && str[j] != ' '; j++)
            continue;
        reverse_mem(str + i, j - i);
    }
    reverse_mem(str, size);
}

int main(void) {
    char arr[] = "My job is coding";
    reverse_words(arr);
    printf("%s\n", arr);
    return 0;
}

由於此語句,您沒有得到預期的輸出。

*new_array = *temp; //2nd time new_array is not pointing previous location  so how will you retrieve data 
   new_array++;// you lost previous data bcz pointers no longer holds previous address

當主循環失敗時,new_array指針將指向最后一個,但它應保留起始地址。 而不是直接增加new_array就像

* (new_array + j) =  *temp

我將您的代碼修改為。

void reverse_words(char *arr,int size)
{
        char * ptr = arr;
        char *new_array = (char*)malloc(sizeof(char*) * size);
        printf("initial : %u \n",ptr);
        while(*ptr != '\0')
        {
                ptr++;
        }
        int j=0;
        ptr--;
        printf("first : %c : %u  \n",*ptr,ptr);
        for(int i = size-1;i >=0;i--)
        {
                if(*ptr != ' ' ) //&& i!=0)
                {
                        ptr--;//from last to back

                }
                else
                {
                        char * temp = ptr;
                        temp++;
                        while(*temp != '\0' && *temp!=' ')
                        {
                                new_array[j++]=*temp;
                                temp++;

                        }
                        if(i!=0)
                        {
                                new_array[j++] = *ptr;
                                ptr--;
                        }
                }

        }
        new_array[j] = '\0';
        printf("new = %s \n",new_array);
        strcpy(arr,new_array);

}

我希望您能解決問題,修改first單詞的條件,因為一旦i變為0您就不會為此寫任何邏輯。

我的解決方案根據您的要求

void reverse_words(char *arr,int size) {
        char *new_array = malloc(size * sizeof(char));
        int i,j,k=0;
        for(i = size-1 ;i >= -1 ; i--) {
                if(arr[i] ==' ' || i == -1)
                {
                        for(j=i+1;arr[j]!=' ' && arr[j]!='\0'; j++)
                                new_array[k++] = arr[j];
                        new_array[k++] = ' ';
                }
        }
        new_array[k] = '\0';
        strcpy(arr,new_array);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM