Click here to Skip to main content
Rate this: bad
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 <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");
void printRevFromTo(char* str ,int num, int len){
    if(!num) puts(str);
Posted 16-Jan-13 9:43am
Edited 16-Jan-13 10:23am
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
Please Sign up or sign in to vote.

Solution 3

(Inspired by deity)
void printRevFromTo(char * str, int left, int right)
    if (right == left)
    printRevFT(str, left, right);
  • 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).
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
Rate this: bad
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.
1. don't use gets[^]: use fgets(...) instead
// *** not ok ***
// *** 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";
// *** 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 ***
// 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!
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
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 | :)
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
Your Filters
0 Sergey Alexandrovich Kryukov 910
1 OriginalGriff 508
2 thatraja 225
3 Abhinav S 218
4 Emre Ataseven 200
0 Sergey Alexandrovich Kryukov 8,142
1 OriginalGriff 4,665
2 Peter Leow 3,774
3 Maciej Los 3,515
4 Er. Puneet Goel 3,107

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