反向使用递归的字符串 [英] Reverse a string using recursion

查看:131
本文介绍了反向使用递归的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从上网本code,但我不能得到整个code。
例如如果(* STR)。这是什么code是什么意思?也可以一个字符串返回?我认为,在主阵列可以被改变
直接在功能,但在这里它被退回。

 #包括LT&;&stdio.h中GT;
#定义MAX 100
字符* getReverse(的char []);诠释主(){    焦炭海峡[MAX],*转;    的printf(请输入任何字符串:);
    scanf函数(%S,STR);    REV = getReverse(STR);    的printf(反向字符串是:%S \\ n \\ n,REV);
    返回0;
}字符* getReverse(字符STR []){    静态INT I = 0;
    静态字符转[MAX];    如果(* STR){
         getReverse(STR + 1);
         转[我++] = *海峡;
    }    返回转;
}


解决方案

这是不是递归的最明显的例子,由于使用了静态变量。希望在code通常似乎很清楚你的,我怀疑是迷惑大家的是,在第一次混乱给我相同的部分。

 如果(* STR){
     getReverse(STR + 1);
     转[我++] = *海峡;
}

所以,一行行。

 如果(* STR){

如果我们还没有达到空终止符。

  getReverse(STR + 1);

呼吁字符串的下一个字符的 getReverse 功能。看来pretty直线前进了这里。但它也好像它实际上可能没有扭转任何东西,因为这是下一行

 转[我++] = *海峡;

我们分配指数 I 在STR和增量 I 但在这里开始的字符是棘手的部分。 I 可能不是你的想法。 getReverse被前 I 递增调用。和 I 是静态的,所以变化将函数调用之间持续存在。所以,让我们说我们有一个5字母的单词,让说马,我们将结束与堆栈6调用 getReverse 。第六届不会做任何事情,因为这是它找到空终止符。诀窍是,我们将会进入有关解决以相反的顺序调用。首先其中str指向E呼叫将解决和增量 I ,因为所有其他的人都还在等待他们的将呼叫 getReverse 返回。所以,最后一个字母实际上是第一批获得增加和增量 I 这就是可能会造成混淆这里。

I got this code from the internet but I couldnt get the whole code. for example if(*str) . What does this code mean? and also can a string be returned? I thought that an array in main can be changed directly in a function but here its been returned..

#include<stdio.h>
#define MAX 100
char* getReverse(char[]);

int main(){

    char str[MAX],*rev;

    printf("Enter  any string: ");
    scanf("%s",str);

    rev = getReverse(str);

    printf("Reversed string is: %s\n\n",rev);
    return 0;
}    

char* getReverse(char str[]){

    static int i=0;
    static char rev[MAX];

    if(*str){
         getReverse(str+1);
         rev[i++] = *str;
    }

    return rev;
}

解决方案

This is not the clearest example of recursion due to the use of the static variables. Hopefully the code generally seems clear to you, I suspect the part that is confusing to you is the same that was confusing to me at first.

if(*str){
     getReverse(str+1);
     rev[i++] = *str;
}

So line by line.

if(*str){

If we have not reached the null terminator.

     getReverse(str+1);

Call the getReverse function on the next character of the string. It seems pretty straight forward up to here. But it also seems like it may not actually reverse anything because this is the next line

     rev[i++] = *str;

We assign index i the character at the beginning of str and increment i but here is the tricky part. i may not be what you think. getReverse gets called before i is incremented. And i is static, so changes will persist between function calls. So, lets say we have a 5 letter word, let say "horse" we will end up with 6 calls on the stack to getReverse. The 6th will not do anything because that is where it finds the null terminator. The trick is that we will then go about resolving the calls in reverse order. First the call where str is pointing to 'e' will resolve and increment i because all the other ones are are still waiting for their calls to getReverse to return. So the last letters are actually the first ones to get added and increment i which is what can be confusing here.

这篇关于反向使用递归的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆