简体   繁体   English

使用递归函数反转字符串

[英]Reversed String Using Recursion Functions

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

void rec(char pin[]);

main()
{
      char pin[100];
      printf("Give word: ");
      scanf("%s", pin);
      rec(pin);
      system("pause");
}

void rec(char pin[])
{
     int i=0;
     if (pin[i]=='\0')
        return;

     else
     {    
        rec(pin[i+1]);
        printf("%c", pin[i]);

     }

}

Well seems not to work but I don't know why. 好吧,似乎行不通,但我不知道为什么。 (I am not allowed to use the for loop, the function strlen and things like that). (不允许使用for循环,strlen函数等)。

in rec function else part you are passing a element which should be address of element.so try this in else part 在rec函数else部分中,您传递的元素应该是element.so的地址,因此请在else部分中尝试此操作

else
     {    
        rec(&pin[i+1]);
        printf("%c", pin[i]);

     }

Well, since your question is "why it doesn't work", might as well answer exactly that. 好吧,既然您的问题是“为什么不起作用”,那么不妨回答一下。

I'm going to assume that the re() declaration is just a typo for rec() -- of course you have to correct that. 我将假设re()声明只是rec()的错字-当然,您必须更正它。

In the first line of that function, you declare a variable, int i = 0; 在该函数的第一行中,您声明一个变量int i = 0; . However, that variable is never assigned to again. 但是,该变量不会再分配给该变量。 Scan the function for any assignment on i -- you won't find any. 扫描功能以查找i上的任何分配-您将找不到任何分配。 Therefore, that i variable is a constant 0. With that in mind, let's replace i by 0 and write the code again: 因此, i变量是常数0。考虑到这一点,让我们将i替换为0并再次编写代码:

if (pin[0]=='\0')
    return;
else
{    
   rec(pin[1]);
   printf("%c", pin[0]);
}

The offending line is clearly rec(pin[1]) . 令人反感的行显然是rec(pin[1]) The function expects a char * argument, ie, a string (note that char * and char [] are the same in function parameter declarations). 该函数需要一个char *参数,即一个字符串(请注意, char *char []在函数参数声明中相同)。 However, pin[1] is just the second character of pin . 但是, pin[1]只是pin的第二个字符。 What you're doing there is converting implicitly that character to a pointer and passing it to the function -- which is incorrect. 您正在执行的操作是将该字符隐式转换为指针,然后将其传递给函数-这是不正确的。

What you want to pass to rec() is the pointer to the second character, since that would make it a pointer to a string beginning at the second character of pin . 您想要传递给rec()是指向第二个字符的指针,因为这将使其成为指向从pin的第二个字符开始的字符串的指针。 So, the correct call would be rec(pin + 1) , not rec(pin[1]) . 因此,正确的调用将是rec(pin + 1) ,而不是rec(pin[1]) Since pin points to the first character of the string, pin + 1 points to the second. 由于pin指向字符串的第一个字符,因此pin + 1指向第二个字符。

This is not correct. 这是不正确的。 First of all, you are using an automatic variable. 首先,您正在使用自动变量。 so, 'i' will always be initialized to 0. 因此,“ i”将始终初始化为0。

use static int i, and see carefully. 使用static int i,并仔细查看。 you are throwing char to char*. 您将char变成char *。 so, you cannot throw rec(pin[i+1]); 因此,您不能抛出rec(pin [i + 1]); change this to rec(pin); 将此更改为rec(pin); and before printf("%c", pin[i]); 并且在printf(“%c”,pin [i])之前; decrement 'i', before calling rec recursively, increment 'i' . 递减调用i之前,先递减“ i”。 Last but not least, you are calling 'rec'. 最后但并非最不重要的一点是,您正在调用“ rec”。 but function name is 're', where is c??? 但是函数名是“ re”,c在哪里?

void rec(char pin[]){
    if (*pin=='\0')
        return;
    else {
        rec(pin + 1);
        printf("%c", *pin);
    }
}

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

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