Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
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:
#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 16-Jan-13 9:43am
idobry427
Edited 16-Jan-13 10:23am
v7
Comments
Sergey Alexandrovich Kryukov at 16-Jan-13 15:57pm
   
First, why recursion?
—SA
CPallini at 16-Jan-13 16:00pm
   
Because: “To iterate is human, to recurse divine.”
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
// *** not ok ***
gets(str);
 
// *** OK ***
fgets(buffer, BUF_SIZE-1, STDIN);
2. don't use a string literal as buffer to store data:
// *** 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.
// *** 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... Wink | ;-) Play with it to learn the effect of head and tail recursion!
 
Cheers
Andi
  Permalink  
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

(Inspired by deity)
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).
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 16-Jan-13 20:24pm
   
Just the inspiration can be enough, a 5.
—SA
Espen Harlinn at 17-Jan-13 16:55pm
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Smile | :) 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. Smile | :)
 
—SA
  Permalink  
v2
Comments
idobry at 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 at 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 at 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 at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 271
1 OriginalGriff 253
2 nv3 50
3 VJ Reddy 45
4 Mehdi Gholam 45
0 Sergey Alexandrovich Kryukov 6,696
1 OriginalGriff 6,149
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100