Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi guys so, my program supposed to print and reverse string only between boundary, for example: original string:hello world from 2 to 9: lrow oll but in my recursion func it got stuck in the first and most simple "if" and idk why even with debugger. here is my script:
C++
#include<stdio.h>
#include <conio.h>
#include <string.h>

void printRevFromTo(char* str ,int num, int len);

void main(){
    char *text = "Experimental text";
    int len = strlen(text);
    printf("*** Reversed from 0 to len:\n");
    printRevFromTo(text, 0, len);
    printf("\n*** Reversed from 0 to len/2:\n");
    printRevFromTo(text, 0, len/2);
    printf("\n*** Reversed from 10 to len:\n");
    printRevFromTo(text, 10, len);
    printf("\n*** Reversed from 22 to 33:\n");
    printRevFromTo(text, 22, 33);
    printf("\n*** Finish.\n");
getch();
}
void printRevFromTo(char* str ,int num, int len){
    if(!num) puts(str);
    printf("%c",str[num]);
    if(len>=num)
    printRevFromTo(str,num+1,len);
 }
Posted
Updated 16-Jan-13 10:23am
v7
Comments
Sergey Alexandrovich Kryukov 16-Jan-13 15:57pm    
First, why recursion?
—SA
CPallini 16-Jan-13 16:00pm    
Because: “To iterate is human, to recurse divine.”
Sergey Alexandrovich Kryukov 16-Jan-13 16:06pm    
Pretty nice. But then, I'll answer: Let the deity solve this problem. :-)
Please see one answer by a mortal one, mine... :-)
—SA

(Inspired by deity)
C#
void printRevFromTo(char * str, int left, int right)
{
    right--;
    if (right == left)
    {
        putchar('\n');
        return;
    }
    putchar(str[right]);
    printRevFT(str, left, right);
}


Prerequisites:
  • str must be a valid pointer to a memory buffer containing printable characters.
  • left and right are valid indices of str (in str boundaries).
  • right>=left.

(If you don't like prerequisites please do your own error handling stuff).
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 16-Jan-13 20:24pm    
Just the inspiration can be enough, a 5.
—SA
Espen Harlinn 17-Jan-13 16:55pm    
5'ed!
You have many flaws in your code.

[EDIT] Now that you have replaced gets by puts, items 1...3 do not apply anymore to your example - they are nonetheless to be considered in general.
[/EDIT]

1. don't use gets[^]: use fgets(...) instead
C
// *** not ok ***
gets(str);

// *** OK ***
fgets(buffer, BUF_SIZE-1, STDIN);

2. don't use a string literal as buffer to store data:
C
// *** not ok ***
char* p = "this is dummy content";
gets(p);

// *** OK ***
const int BUF_SIZE = 50;
char buffer[BUF_SIZE];
fgets(buffer, BUF_SIZE-1, STDIN);
buffer[BUF_SIZE-1] = '\0';

3. Take that magic reading from stdin out of the function. That does not belong there.
4. How do you know the num is not beyond the end of the str?
5. I strongly suggest to add blocks instead of single line in an if...! Especially important for my hint below.
C
// *** not ok ***
if(len>=num)
printRevFromTo(str,num+1,len);

// OK
if (len >= num) {
   printRevFromTo(str, num+1, len);
}

6. This does not print reverse, it prints forward, as if you would printf("%s", str); (without recursion, of course).

Hint: once you have fixed the above flaws, to get the reverse printing, move the printf(...) a bit further down... ;-) Play with it to learn the effect of head and tail recursion!

Cheers
Andi
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 16-Jan-13 20:27pm    
Some good points here, a 5.
Still, I am against pointless exercises. It would be a different story if using recursion was not rational, but challenging, or teaching something important. There are many interesting problems (however, they are hard to invent...) But this case...
I tried to explain my point in my answer.
—SA
Andreas Gieriet 16-Jan-13 23:37pm    
Thanks for your 5!
Well, I have a more relaxed view on that - in the right context, this exercise may just be fine.
Sometimes it's easier to construct a somehow useless application to show some concept (in that case I would say the difference between head and tail recursion). Maybe a better example would be a traversing a tree. But that requires more complex setup which distracts from the realitem of recursion.
Cheers
Andi
This is one of the worst cases for the use of recursion. Actually, text books and school courses are full of example of recursion use which look like they were designed intentionally to confuse students into thinking that recursion is something bad. :-) One of such cases is factorial, but your case is even worse.

Don't do it. Use just the loop. This is really, really easy.

Now, I can imagine that this is your school assignment. In this case, this is your own problem. But, if so, listen to my advice: look carefully at what your teacher teaches you. Maybe, it's time to seek for some alternative ways to get real knowledge. :-)

—SA
 
Share this answer
 
v2
Comments
idobry 16-Jan-13 16:09pm    
You were right this is an assignment from my university, and now it's all about recursion so even if its not the right case for it i cant do a thing about it.
Sergey Alexandrovich Kryukov 16-Jan-13 16:13pm    
I mention the possibility of assignment. Hm. University... I can imaging that. And I'm not sure that you are right about "can't do a thing about it". I always refused to do what I though was wrong, but still alive... Anyway, please deal with that by yourself. Or show my answer to your teacher...
:-)
H.Brydon 18-Jan-13 10:33am    
I disagree. Solving "pointless" problems is reasonable for learning concepts, where the concepts can be used in more complex situations that are too onerous for the learning process.
Sergey Alexandrovich Kryukov 18-Jan-13 13:02pm    
I would probably agree with you, but there are two "buts":

1) I did not mean "pointless" problems which are theoretically important, or touch deep fundamental principles. Some problem are "pointless" in terms of practice, but still very significant in terms as theoretical problem, or even well-solved problems, but still fundamental; such problem are not pointless. I don't thing the OP's problems can be considered good in any of these aspects.

2) There are many, many much better problems. I know because I go in for inventing them. This is real hard. The problem I can see in this post is the lazy-man inventions. Just poor. There are a great deal of really good problems with the use of recursion. I hope you know some by yourself.

And this is the problem. Most interview questions are way too poor to be useful for selecting candidates. Majority of interviewers I ever saw are way to underqualified for the job.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900