簡體   English   中英

C字符串已反轉

[英]C String Reversed

我正在寫一個簡單的c程序,它反轉一個字符串,從argv [1]中取出字符串。 這是代碼:

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

char* flip_string(char *string){
    int i = strlen(string);
    int j = 0;

    // Doesn't really matter all I wanted was the same size string for temp.
    char* temp = string;
    puts("This is the original string");
    puts(string);
    puts("This is the \"temp\" string");
    puts(temp);

    for(i; i>=0; i--){
        temp[j] = string[i]
        if (j <= strlen(string)) {
            j++;
        }
    }

    return(temp);
}

int main(int argc, char *argv[]){
    puts(flip_string(argv[1]));
    printf("This is the end of the program\n");
}

基本上就是這樣,程序編譯並且一切都沒有返回到最后的臨時字符串(只是空格)。 在開始時,當它等於字符串時,它會打印temp。 此外,如果我在for循環中使用temp的字符printf執行字符,則打印正確的臨時字符串即字符串 - >反轉。 就在我嘗試將其打印到標准輸出時(在for循環之后/或在主循環中)沒有任何反應只打印空白區域。

謝謝

你正朝着正確的方向試圖使用指針。 再多想一想,你可能會有它。 安全實施如下:

#include <stdio.h>

char *flip_string(char *str)
{
    char *lhs = str, *rhs = str;
    if (!str || !*str || !*(str+1))
        return str;

    while (*++rhs); // rhs to eos
    while (lhs < --rhs)
    {
        char tmp = *lhs;
        *lhs++ = *rhs;
        *rhs = tmp;
    }
    return str;
}

int main()
{
    char test1[] = "Hello, World!";
    char test2[] = "";
    char test3[] = "1";
    printf("%s, %s, %s\n", flip_string(test1), flip_string(test2), flip_string(test3));
    return 0;
}

產量

!dlroW ,olleH, , 1

希望能幫助到你。

您的函數似乎執行“就地反轉”,即它替換內存中的給定字符串。 這樣做時,請確保不要將尾隨零移動到前面。 這是函數的簡單實現

#include <assert.h>
#include <string.h>

void flip_string(char *s)
{
    assert(s);
    char *t = strchr(s, '\0') - 1;
    for (; s < t; ++s, --t) {
        char tmp = *s;
        *s = *t;
        *t = tmp;
    }
}

該函數斷言它獲取一個字符串(即不是空指針)並且該內存是可寫的。 然后它設置一個t指針指向字符串的最后一個字符 - 通過strchr執行此strchr而不是編寫手動循環有一個好處,即strchr通常是一個高度優化的函數,它不會以單字節步驟遍歷字符串而是一次考慮四個甚至更多的字節。 它也可以說更具表現力。

然后主循環交換由st引用的字符(即最初是第一個和最后一個字符),然后向前/向后移動指針直到它們相遇。

該函數更簡潔,因為它不需要保留傳入的原始指針,而是可以直接修改s 這是決定一個函數應該要么修改其參數或者返回一個新值的結果-而不是兩個。 兩者都意味着您可以調用函數

printf("%s", flip_string(s));

......這完全掩蓋了s實際上是被修改過的。

flip_string(s);
printf("%s", s);

在這方面更明確。

在這里,你顯然希望temp是另一個變量而不是string 但是,您進行的初始化將導致兩個指針指向同一位置。

你應該做的是:

char *flip_string(const char *string)
{
    char *tmp = NULL;
    size_t len = strlen(string);
    int i = 0;
    /*
     * Don't forget that strlen() returns the length of the string
     * **WITHOUT** counting the ending '\0' character.
     * Thus, you have to add the extra space when you're allocating
     */
    if (!(tmp = malloc(len + 1))) {
        printf("Allocation failed ...\n");
        return NULL;
    }
    /*
     * The newly created string must have an ending character
     * Which is '\0'
     */

    tmp[len] = '\0';

    /*
     * Here, you want to reverse the "true content" of your string.
     * That is why you ignore the ending '\0' of the string by
     * setting the upper bound to strlen (with a strict '<') **AND**
     * it is also the reason a '- 1' just pops in the index choice.
     */
    for(i = 0; i < len; i++) {
        tmp[i] = string[len - i - 1];
    }
    return tmp;
}

正如WhozCraig強調的那樣,有一種替代解決方案,它只需修改參數字符串而無需分配內存:

void flip_string(char *s)
{
    size_t len = strlen(s);
    char *p = s + len - 1;
    while (s < p) {
        *p ^= *s;
        *s ^= *p;
        *p ^= *s;
        p--;
        s++;
    }
}

注意XOR技巧,以避免為交換字符使用臨時存儲變量( ^C中XOR運算符)

暫無
暫無

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

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