繁体   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