Use Recursion To Print String In Reverse | C Programming Example

Поделиться
HTML-код
  • Опубликовано: 9 янв 2025

Комментарии • 45

  • @franciscocarneiro
    @franciscocarneiro 2 года назад +15

    Thanks. I understood more about recursion through your short example than a 2 hours lecture that I had recently....

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      Thank you for the positive feedback Francisco! I'm really glad to hear the video helped you understand recursion better. 😀

  • @momohwilliams2148
    @momohwilliams2148 2 года назад +5

    This channel is excellent!
    Thank you

  • @kloul
    @kloul 2 года назад +4

    So any recursive function works from last call to the first? Because i had something similar but in a "for" cicle but had to do it like with -1 and you do it with +1

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +3

      That's a really good question. In some sense yes, ultimately for recursion to work the "later" function calls need to return to the "earlier" function calls. But we can have some pretty complex recursion where we have a tree of recursive function calls, and in that sense there isn't always a clear last and first even though there is an order (but it's an order that resembles a tree). And in the same way a loop can work on some problems from front to back or back to front, recursion could do the same even if the function calls have a certain order. So I could see this working with either +1 or -1, if for example we start at the null terminator and somehow work our way back.

  • @impaglg
    @impaglg 2 года назад

    Thank you for the clear explanation! I finally understand now!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      You're welcome! :-) I'm so glad to hear this video helped you out!

  • @syedsharyarjavaid9853
    @syedsharyarjavaid9853 2 года назад +2

    shouldn't this recursion function stop at null character? because then we don't pass the conditional statement so how are we printing the characters and going back to s[0]? how?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      I left this answer on a similar question below just yesterday, maybe this will help explain things for you as well? :-)
      It's because we call the function first and then after print the character:
      print_reverse(s + 1);
      printf("%c", *s);
      I'll try to illustrate what's gong on with pseudocode, so if we have print_reverse("abc") then we get:
      print_reverse("bc") (essentially....)
      print("a")
      but then print_reverse("bc") also does the same thing as the above, and so we can think of it as being replaced with the first two lines below:
      print_reverse("c")
      print("b")
      print("a")
      then print_reverse("c") will be called and we'll have:
      print("c")
      print("b")
      print("a")
      and recursion will stop because we reach the end of the string, at that point the above prints will occur in order and we'll get "cba".

    • @syedsharyarjavaid9853
      @syedsharyarjavaid9853 2 года назад

      @@PortfolioCourses got it! thank you!

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      @@syedsharyarjavaid9853 You're welcome! 🙂

  • @dynamicgamer9727
    @dynamicgamer9727 Год назад

    Y we have used underscore after print_reverse

  • @anshulc3941
    @anshulc3941 2 года назад +1

    Easy to understand !

  • @rotrot-yd1zd
    @rotrot-yd1zd 2 года назад

    void _puts_recursion(char *s)
    {
    if (*s != '\0')
    {
    _putchar(*s);
    s ++;
    _puts_recursion(s);
    }
    }
    in this function, where do i place '
    ' , so that it doesnt recursively have a next line after each character

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      Hmm, I'm not sure we can put a '
      ' somewhere and not have it show up with each recursive call. Maybe you could try having an else case though?
      ...
      else _putchar('
      ');
      That might have the
      only appear when *s does equal '\0', i.e. the end of the string?

  • @ishimwechris3978
    @ishimwechris3978 2 года назад

    yeah its very easy to understand
    but i have a question, i am beginner so if i make a mistake understand
    does the (char *s in void) work as a pointer

    • @ishimwechris3978
      @ishimwechris3978 2 года назад

      never mind after going through the video again i think i understand it

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      Ok glad to hear that Ishimwe! :-)

  • @josephjohn4810
    @josephjohn4810 2 года назад

    why does all the function calls get unrolled and starts printing backward?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +2

      Great question Joseph! :-) It's because we call the function *first* and then after print the character:
      print_reverse(s + 1);
      printf("%c", *s);
      I'll try to illustrate what's gong on with pseudocode, so if we have print_reverse("abc") then we get:
      print_reverse("bc") (essentially....)
      print("a")
      but then print_reverse("bc") also does the same thing as the above, and so we can think of it as being replaced with the first two lines below:
      print_reverse("c")
      print("b")
      print("a")
      then print_reverse("c") will be called and we'll have:
      print("c")
      print("b")
      print("a")
      and recursion will stop because we reach the end of the string, at that point the above prints will occur in order and we'll get "cba".

    • @josephjohn4810
      @josephjohn4810 2 года назад +1

      @@PortfolioCourses great explanation. Thanks

    • @PortfolioCourses
      @PortfolioCourses  2 года назад +1

      You're welcome! :-)

  • @jacobmarshall379
    @jacobmarshall379 2 года назад

    what would you do if you wanted to include the
    in the function itself?

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      Good question Jacob, I suspect you could do something like this in the function body...
      if (*s != '\0')
      {
      print_reverse(s + 1);
      printf("%c", *s);
      }
      else printf("
      ");

    • @jacobmarshall379
      @jacobmarshall379 2 года назад

      @@PortfolioCourses thanks for the reply, that's what I thought as well but the output puts the new line before the text, i guess im not exactly sure what this is doing.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      @@jacobmarshall379 Hmm, does the string itself end with a newline? If so that would be output first.

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      @@jacobmarshall379 Feel free to copy/paste your code into a comment here.

    • @jacobmarshall379
      @jacobmarshall379 2 года назад

      @@PortfolioCourses void print_rev(char *s)
      {
      if (*s != '\0')
      {
      print_rev(s + 1);
      putchar(*s);
      }
      else
      {
      putchar('
      ');
      }
      }
      int main(void)
      {
      char *s;
      s="hello there";
      print_rev(s);
      return(0);
      }

  • @gideonokyeremensah6647
    @gideonokyeremensah6647 2 года назад

    Well explained thanks

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      You're welcome Gideon, I'm glad you enjoyed it! :-D

  • @Lua-t6e
    @Lua-t6e Год назад

    Great work

  • @synczx3023
    @synczx3023 2 года назад +3

    This is the way

  • @mawhadmd
    @mawhadmd 9 месяцев назад

    It is indeed tricky,

  • @joshuavasquez7787
    @joshuavasquez7787 2 года назад

    Can you do one where you return a char* from the function. so like...
    char* print_reverse(char *s)
    {
    }

    • @PortfolioCourses
      @PortfolioCourses  2 года назад

      Probably, especially if we were willing to add some parameters. If we are just printing the string I'm not sure why we would want to return a pointer though, and what the pointer would point to. We could make it point to the last character or something though, most likely.

  • @angelffg
    @angelffg 8 месяцев назад

    Great!

  • @williamcatlovrr9572
    @williamcatlovrr9572 2 года назад

    nice vid yo