将源字符串反转到目标

问题描述 投票:0回答:3

我试图反转一个字符串并将反转的字符串写入目标。我必须使用相同的基本结构,并且无法弄清楚我的反向功能有什么问题。

void reverse(const char source[], char destination[]) {

if(source = 0){
    return;
}
else
{
    reverse(source+1, destination);
    destination[strlen(source)-1]=source;
}

}

void testingReverse() {
    const char *sources[] = {
    "Welcome to CMPSC 311!",
    "abcdef",
    "_ ab  cd  ef *"
};
    const char *expected[] = {
    "!113 CSPMC ot emocleW",
    "fedcba",
    "* fe  dc  ba _"
};
    char destination[50];
    int i;
    for (i = 0; i < 3; i++) {
        printf("source: \"%s\"\n", sources[i]);
        memset(destination, 'x', 49);
        destination[49] = 0;
        reverse(sources[i], destination);
        printf("destination: \"%.49s\"\n", destination);
        if (strcmp(destination, expected[i]) == 0) {
            printf("----pass----\n");
        }
        else {
            printf("----fail----\n");
        }
    }
}

以下是我现在得到的结果。

Testing reverse:

source: "Welcome to CMPSC 311!"

Segmentation fault: 11
c
3个回答
0
投票

试试这个

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

void reverse(const char *source ,  char *destination) 
{
int i=0;
int length=0;

length=strlen(source);

if(!length) return;

strcpy( destination , source);

   while(source[i])
   { 
       printf("\n%c",source[i]);
       destination[length-(1+i)]=source[i];
       printf("\n=====> %s",destination);
       i++;
   }

printf("\n%s", destination);

}

int main()
{
   char string[99];
   char destination[99];

    strcpy( string, "abcdefghijklmnop");
    reverse(string, destination);
    }

1
投票

你可能会过度复杂地扭转一个字符串。您需要知道的是源字符串的长度(并且它将适合目标),然后在它最基本的形式,您可以使用索引,例如使用简单的for循环将源反转为dest。

void reverse (char *dst, const char *src)
{
    size_t i, j = strlen(src);

    for (i = 0; src[i]; i++)        /* loop over each char in src */
        dst[i] = src[--j];          /*  decrement j, reverse copy to dst */

    dst[i] = src[i];                /* nul-terminate dst */
}

(注意:srcdst参数的顺序切换为与其他字符串函数一致,如strcpy等。)

现在这还远未完成。您需要/需要其他验证以确保您的参数有效,并且反向源字符串将适合所提供的目标缓冲区。添加验证后,您可以执行以下操作,以使事情更加健壮,例如:

#define MAXC 1024

void reverse (char *dst, const char *src)
{
    size_t i, j;

    /* validate src and dest not NULL */
    if (src == NULL || dst == NULL) {
        fputs ("error: NULL paraemter passed.\n", stderr);
        return;
    }

    j = strlen(src);                /* get index of last char in src */

    if (j >= MAXC) { /* validate reversed src fits in dst */
        fputs ("error: src exceeds MAXC - 1.\n", stderr);
        *dst = 0;   /* set dst to empty-string */
        return;
    }

    for (i = 0; src[i]; i++)        /* loop over each char in src */
        dst[i] = src[--j];          /* decrement j, reverse copy to dst */

    dst[i] = src[i];                /* nul-terminate dst */
}

添加所需的#include文件和一个简短的示例程序,该程序将反转作为程序的第一个参数提供的字符串(或者如果没有提供参数则默认反转"Welcome to CMPSC 311!"),您可以执行以下操作:

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

#define MAXC 1024
...
int main (int argc, char **argv)
{
    char *s = argc > 1 ? argv[1] : "Welcome to CMPSC 311!",
        rev[MAXC];

    reverse (rev, s);

    printf ("original: %s\nreversed: %s\n", s, rev);
}

示例使用/输出

$ ./bin/reverse
original: Welcome to CMPSC 311!
reversed: !113 CSPMC ot emocleW

或者,将字符串作为输入传递给程序:

$ ./bin/reverse "My dog has fleas and my cat has none."
original: My dog has fleas and my cat has none.
reversed: .enon sah tac ym dna saelf sah god yM

仔细看看,如果您有其他问题,请告诉我。


0
投票

也许这会给你灵感

void reverse(char *str) { 
    if (*str) { 
       reverse(str+1); 
       printf("%c", *str); 
    } 
} 

你也可以看看strrev(arr);

https://www.programmingsimplified.com/c-program-reverse-string

© www.soinside.com 2019 - 2024. All rights reserved.