简体   繁体   English

使用递归反转数组内容(C 语言)

[英]Reverse array content using recursion (C Language)

Function reverseAr() takes in two arguments, an array of char (ar[]) and the size of the array (n), and then reverses the content of the array (without the use of another array).函数 reverseAr() 接受两个参数,一个字符数组 (ar[]) 和数组的大小 (n),然后反转数组的内容(不使用另一个数组)。 The function will then return the result to the calling function through parameter ar.然后,该函数将通过参数 ar 将结果返回给调用函数。

My code can't seem to run, I think I'm passing the values in between the functions wrongly, but I can't figure out where.我的代码似乎无法运行,我想我在函数之间错误地传递了值,但我不知道在哪里。

Here's the code:这是代码:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 
#include <string.h> 
#include <math.h> 

void reverseAr(char ar[], int n);

int main()
{
    char str[128];
    int len;

    printf("\nEnter an array of characters: ");
    gets(str);
    len = strlen(str);
    printf("The reversed array of characters is = %s\n", reverseAr(str, len));

    return 0;
}

void reverseAr(char ar[], int n)
{
    char temp = ar[0];
    ar[0] = ar[n - 1];
    ar[n - 1] = temp;
    if (n > 1) 
    {
        reverseAr(++ar, n - 2);
    }
}

Expected output:预期输出:

Enter an array of characters: abcde
The reversed array of characters is = edcba

This:这个:

char str;

gets(str);

is undefined behavior, and should give compiler warnings (or even errors) that you are ignoring.是未定义的行为,应该给出您忽略的编译器警告(甚至错误)。

gets() requires a pointer to an array of characters, you're giving it the value of a single character, which is not the same thing at all. gets()需要一个指向字符数组的指针,你给它一个单个字符的值,这根本不是一回事。 It will interpret the undefined value of that character as an address, and store the input text there.它将将该字符的未定义值解释为地址,并将输入文本存储在那里。

It should be:它应该是:

int main()
{
  char line[128];

  printf("\nEnter an array of characters: ");
  if(fgets(line, sizeof line, stdin) != NULL)
  {
    const size_t len = strlen(line);
    printf("The reversed array of characters is = %s\n", reverseAr(line, len));
  }
  return 0;    
}

the return type of strlen() is not int , it's size_t so you should use that, and change reverseAr() accordingly. strlen()的返回类型不是int ,它是size_t所以你应该使用它,并相应地改变reverseAr()

This is the working code for reversing string这是反转字符串的工作代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h> 
#include <string.h> 
#include <math.h> 

int reverseAr(char ar[], int , int);

int main()
{
    char str[128];
    int len;

    printf("\nEnter an array of characters: ");

    if(fgets(str, sizeof(str), stdin) != NULL)
    {
        len = strlen(str);
        reverseAr(str, 0, len-1);
    }
    printf("The reversed array of characters is = %s\n", str );
    system("pause");
    return 0;
}

int reverseAr(char ar[], int first, int last)
{
    char temp = ar[first];
    ar[first] = ar[last];
    ar[last] = temp;

    if (first < last) 
    {
        reverseAr(ar, ++first, --last);
    }
    else
    {
        return 0;
    }
}

I think the problem is how you print the result:我认为问题在于你如何打印结果:

void reverseAr(char ar[], int n);
printf("The reversed array of characters is = %s\n", reverseAr(str, len));

printf expects a string and you are passing it void... printf 需要一个字符串,而您将它传递给 void ......

You should replace that with:您应该将其替换为:

reverseAr(str, len-1);
printf("The reversed array of characters is = %s\n", str);

You will notice I am passing len-1 insted of len to reverseAr that is because you want to leave the last character in the string ('\\0') alone, and only switch the rest of the characters.你会注意到我传递LEN-1 insted的的lenreverseAr那是因为你要离开的字符串(“\\ 0”)独自一人在最后一个字符,只有切换字符的其余部分。 Ultimately you want to fix the reverseAr function as well:最终,您还想修复reverseAr函数:

void reverseAr(char ar[], int i, int n)
{
    char temp = ar[i];
    ar[i] = ar[n];
    ar[n] = t;

    if (i < n) 
    {
        reverseAr(ar, i++, n - 1);
    }
}

And call it a differently: reverseAr(str,0,len-1); reverseAr(str,0,len-1);说法: reverseAr(str,0,len-1);

You can use a pointer instead of using the array name to increment (as ++ar is not valid) in the reverseAr function.您可以使用指针而不是使用数组名称在reverseAr函数中递增(因为++ar无效)。

The following code should work:以下代码应该可以工作:

/* In Main */

int main()
{
        char str[128];
        int len;

        printf("\nEnter an array of characters: ");
        gets(str);    /* use fgets instead */

        len = strlen(str) - 1;  /* length is one less than the strlen */

        printf("The reversed array of characters is = %s\n", reverseAr(str, len));

        return 0;
}

void reverseAr(char *ar, int n)
{
        /* Base case */
        if (ar > (ar + n))
        {
            return;
        }
        char temp = *ar;
        *ar = *(ar + n);
        *(ar + n) = temp;
        reverseAr(ar + 1, n - 1);
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM