簡體   English   中英

循環以反轉C中的字符串

[英]loop to reverse string in C

所以我四處查看,所以找不到能回答我問題的代碼。 我編寫了一個函數,該函數應該將字符串反轉為cmd行中的輸入。 這是函數:

void reverse (char string[]) {
    int x;
    int i = 0;
    char line[strlen(string)];

    for (x = strlen(string) - 1; x > 0; x--) {
        char tmp = string[x];
        line[i] = tmp;
        i++;
    }
    string = line;
}

當我調用reverse()函數時,字符串保持不變。 即,“ abc”仍為“ abc”

如果需要更多信息或問題不適當,請告訴我。

謝謝!!

您聲明的line數組要短一個char請記住null處為null

還有一點,應該for (x = strlen(string) - 1; x >= 0; x--)因為您需要將字符復制到0

void reverse (char string[]) {
    int x;
    int i = 0;
    char line[strlen(string) + 1];

    for (x = strlen(string) - 1; x >= 0; x--) {
        char tmp = string[x];
        line[i] = tmp;
        i++;
    }

    for(x = 0; x < strlen(string); x++)
    {
        string[x] = line[x];
    }
}

請注意,此函數在傳遞空字符串或字符串文字時將導致啟示(如Bobby Sacamano所說)。

建議您可以執行以下操作: void reverse(char source[], char[] dest)並檢查源字符串是否為空。

代碼的最后一行不執行任何操作string = line;

參數按值傳遞,因此,如果更改其值,則該值僅在函數本地。 指針是它們所指向的內存地址的值。 如果要修改傳遞函數的指針,則需要使用指向該指針的指針。

這是一個簡單的例子。

void reverse (char **string) {
    char line = malloc(strlen(*string) + 1);
    //automatic arrays are deallocated once the function ends
    //so line needs to be dynamically or statically allocated

   // do something to line

    *string = line;
}

顯而易見的問題是,您可以使用靜態內存初始化字符串,然后此方法將用動態內存替換靜態內存,然后必須釋放動態內存。 這在功能上沒有什么錯,只是有點危險,因為不小心釋放字符串文字是非法的。

char *test = "hello";
reverse(test);
free(test); //this is pretty scary

同樣,如果將測試分配為動態內存,則指向它的指針將丟失,然后將成為內存泄漏。

我認為您的答案幾乎是正確的。 您實際上不需要為行中的空字符添加額外的插槽。 您只需要進行兩個小更改:

  • 將過程底部的賦值語句更改為memcpy。
  • 將循環條件更改為<-

因此,您的正確代碼是這樣的:

void reverse (char string[]) {
  int x;
  int i = 0;
  char line[strlen(string)];

  for (x = strlen(string) - 1; x >= 0; x--) {
    char tmp = string[x];
    line[i] = tmp;
    i++;
  }
  memcpy(string, line, sizeof(char) * strlen(line));
}

由於要反轉字符串,因此首先必須確定是要反轉字符串的副本,還是要就地(就地)反轉字符串。 由於您是在'C'上下文中詢問此問題的,因此,假設您要保留原始字符串,則要更改現有字符串(反轉現有字符串)並在調用函數中復制該字符串。

您將需要字符串庫

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

數組索引有效,而該版本采用了這種方法,

/* this first version uses array indexing */
char*
streverse_a(char string[])
{
    int len; /*how big is your string*/
    int ndx; /*because 'i' is hard to search for*/
    char tmp; /*hold character to swap*/
    if(!string) return(string); /*avoid NULL*/
    if( (len=strlen(string)) < 2 ) return(string); /*one and done*/
    for( ndx=0; ndx<len/2; ndx++ ) {
        tmp=string[ndx];
        string[ndx]=string[len-1-ndx];
        string[len-1-ndx]=tmp;
    }
    return(string);
}

但是您可以對指針執行相同操作

/* this is how K&R would write the function with pointers */
char*
streverse(char* sp)
{
    int len, ndx; /*how big is your string */
    char tmp, *bp, *ep; /*pointers to begin/end, swap temporary*/
    if(!sp) return(sp); /*avoid NULL*/
    if( (len=strlen(bp=sp)) < 2 ) return(sp); /*one and done*/
    for( ep=bp+len-1; bp<ep; bp++, ep-- ) {
        tmp=*bp; *bp=*ep; *ep=tmp; /*swap*/
    }
    return(sp);
}

(不,實際上,編譯器不會為返回void收取更多費用。)

而且由於您始終會測試代碼,

char s[][100] = {
    "", "A", "AB", "ABC", "ABCD", "ABCDE",
    "hello, world", "goodbye, cruel world", "pwnz0r3d", "enough"
};

int
main()
{
    /* suppose your string is declared as 'a' */
    char a[100];
    strcpy(a,"reverse string");

    /*make a copy of 'a', declared the same as a[]*/
    char b[100];
    strcpy(b,a);
    streverse_a(b);
    printf("a:%s, r:%s\n",a,b);

    /*duplicate 'a'*/
    char *rp = strdup(a);
    streverse(rp);
    printf("a:%s, r:%s\n",a,rp);
    free(rp);

    int ndx;
    for( ndx=0; ndx<10; ++ndx ) {
        /*make a copy of 's', declared the same as s[]*/
        char b[100];
        strcpy(b,s[ndx]);
        streverse_a(b);
        printf("s:%s, r:%s\n",s[ndx],b);

        /*duplicate 's'*/
        char *rp = strdup(s[ndx]);
        streverse(rp);
        printf("s:%s, r:%s\n",s[ndx],rp);
        free(rp);
    }
}

暫無
暫無

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

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