[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 430
  • Last Modified:

Recursive Function with Argument as array..!

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.
2 Solutions
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

#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;

To chip3d
Please do not give solution of homework questions.
Ahm, sorry...
sraziAuthor Commented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now