Recursive Function with Argument as array..!

Posted on 2006-04-08
Last Modified: 2012-06-27
Hi all..
I'm looking for the solution of the following problem..!

I need to write a recursive function that has as arguments an array of characters and two bounds on array indices. The function should reverse the order of those entries in the array whose indices are between the two bounds. For example, suppose that the array is :
a[5] = “abcde”;
and the bounds are 1 and 3. Then after the function is run the array elements should be “adcbe”.

Embed the function in a program and test it. To simplify grading, set up your character array as the whole alphabet:

char array[]=“abcdefghijklmnopqrstuvwxyz”;

And allow the user to specify bounds. Be sure to test our program using bounds that have both an even and an odd number of characters between them

Hope someone will help me with this.... Thanks in Advance.
Question by:srazi
    LVL 9

    Assisted Solution

    I can't post a code, because the task looks like a homework.

    The function you need to write is

    // "a" is the array of characers
    // "first" and "last" are the lower and upper bounds on the indices.
    void reverse_interval(char *a, int first, int last) {
        reverse(a+first, a+last);

    void reverse(char *first, char *last) {
        // check if the pointer to the first character is before the pointer to the last character
        // otherwise return from the function

        // swap the *first and the *last characters
        // call the function recursively with first+1 and last-1 parameters
    LVL 4

    Accepted Solution


    #inlucde <iostream>

    // the recursive function is very simple
    void reverse(char *str, int beg, int end)
      // cancel condition
      if (beg >= end) return;
      //swap beg, end
      char tmp = str[beg];
      str[beg] = str[end];
      str[end] = tmp;
      // recursive call
      reverse(str, beg+1, end-1);

    // nicer would be just to give two char pointers
    void reverse(char *beg, char *end)
      // cancel condition
      if (beg >= end) return;
      //swap beg, end
      char tmp = *beg;
      *beg = *end;
      *end = tmp;
      // recursive call
      reverse(beg+1, end-1);

    int main()
      // to specify the bounds by user:
      int beg, end;
      std::cout << "specify inner bound: ";
      std::cin >> beg;
      std::cout << "specify outer bound: ";
      std::cin >> end;
      char text[] = "abcdefghijklmnopqrstuvwxyz";
      reverse(text, beg, end);
      // to call reverse with two parameters:
      // reverse(text+beg, text+end);
      std::cout << text;
      return 0;

    LVL 12

    Expert Comment

    To chip3d
    Please do not give solution of homework questions.
    LVL 4

    Expert Comment

    Ahm, sorry...

    Author Comment

    Hi.. Everyone..
    Actually I should be sorry at the first place and not chip3d, since I have asked a question which certainly is a homework for one of my friends, due to unawareness, rajeev_devin is quite right, but I still appreciate if someone atleast gives the Function code (as pallosp in this case) if not the full solution for the homework, since it'll help learn the subject fast or give some reading references or live example for the said subject.

    And would like to know where to get paid assistance to complete the programming tasks and projects... ?

    Thanks n Regards

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Article by: SunnyDark
    This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
    Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now