Link to home
Start Free TrialLog in
Avatar of likempeachesnow
likempeachesnow

asked on

writing a simple program(recursive)

hi, i need help with a problem for class. i have to write a recursive function that takes a character array containing a string as a argument, prints the string backwards and returns nothing. the function should stop processing and return when the terminating null character is encountered. i'm in my first term in school, so it would be really nice if someone could help me out.
Avatar of mblat
mblat

it's against policy of this board to do someone's homework.  I suggest you start doing it and when you run into the problem - ask specific question....

I know it sucks, but hey - it is YOUR homework....
Avatar of likempeachesnow

ASKER

thankx mblat, i just need to know if this looks like i'm going in the right direction.
#include<iostream>

using std::cout;
using std::cin;
using std::endl;

void stringReverse(char,char,char);

void main()
{
     const char printThis = 9;
     char name[printThis]={'S','t','e','p','h','a','n','i','e'};
     char x;
     cout<<"At start: ";
     for(x=0;x<printThis[x] !='\0';x++)
          cout<<name[x]<<' ';
     cout<<endl;
     stringReverse(name,0,printThis-1);
     cout<<"At end: ";
     for(x=0;x>printThis;++x)
          cout<<name[x]<<' ';
     cout<<endl;
     return 0;
}

void stringReverse(char name[],char start,char stop)
{
     char midpoint;
     if(start,stop)
     {
          midpoint=splitList(name,start,stop);
          stringReverse(name,start,midpoint-1);
          stringReverse(name,midpoint+1,stop);
     }
}
ASKER CERTIFIED SOLUTION
Avatar of mblat
mblat

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry - forgot to remove something from main - should look like:

int main()
{
    char* name = "This is the test";
    char x;
    cout<<"At start: ";
    for(x=0; name[x] !='\0'; x++)
         cout<<name[x]<<' ';
   
    cout<<endl;

    stringReverse(name,0);
    cout<<"At end: ";
   
    getch();
    return 0;
}
thankx alot mblat. i just want to let you know that this site has encouraged me to continue in programming. especially knowing that there are people out there like you to lend a helping hand!!!thankx once again!!
when the question say a character array, I think it probably mean char[] and not char*

i think there is no need to make use of the standard library, it complicates things especially so if his teacher had not come to the topic of standard library

a better solution would be :

#include <iostream>
using namespace std;

void stringReverse(const char* name) {
  if (*name) {
    stringReverse(name + 1);
    cout << *name;
  }
}

int main() {
  char printThis[] = {'S','t','e','p','h','a','n','i','e',NULL};

  cout << "At start: " << printThis << endl;
  cout << "At end  : "; stringReverse(printThis); cout << endl;
  return NULL;
}


see? neat and tidy.
I wish to point out a few things in mblat code...

--------- snip ----------------

for(x=0; name[x] !='\0'; x++)
        cout<<name[x]<<' ';

--------- snip ----------------

since name is already null terminated, you can just do a
cout << name; straight away, i see no point trying to separate the letters with a space. of course he may have his reasons which dumbfound me and in that case would much need some enlightenment. (oops i see this in your original code also, so i guess mblat is just trying to follow you)

--------- snip ----------------

std::reverse(str.begin(),str.end());

--------- snip ----------------

std::reverse reverses the elements in a sequence, so that the last element becomes the new first, and the first element the new last. In other words, the above code already reverses the string, you achieved what you want (reversed the code), oh well not exactly except that you did not do it in a recursive way.

--------- snip ----------------

if(!name[start])
       return;

--------- snip ----------------

don't you think that it is better to use

if(name[start]) {
     .
     .
     .
} else return;

let me explain the former code is placed below most of the other code. that means the std::reverse and etc. are executed even before the condition is checked for. what if the null terminator is already reached? but for the latter version, the code is only executed if the null terminator is not reached.

one more thing if you try to run mblat code, you will find out that it prints out the reverse of the string a number of times with each new line cutting away a letter from the beginning of the original word. is this supposed to be like that? either i misinterpreted the question or him...
2 CoolBreeze.

Well as far as

for(x=0; name[x] !='\0'; x++)
       cout<<name[x]<<' ';

I just copied provided code.

as far as  
don't you think that it is better to use

if(name[start]) {
    .
    .
    .
} else return;

In general it is matter of preference.  While I agree that it this case you can cut execution time it is hardly the point here.  
As far as use of STL - I actually think earlier one starts - better.  STL is THING IN ITSELF and it makes one thing quite differently - so it may help him at least to know that such thibng exist....
And finally I didn't want to provide "perfect" code (if there is such a thing) just wanted to put him on a right track, keeping as much of his original code as possible...

Peace :-)
2 mblat

one more question, can explain to me ...
to me the question seems that it just want to print out the string in reverse isn't it?
using std::reverse does the job but it's not recursive
Well, if I understood question correctly ( and from the fast accepted answer I probably did :-) ) then what the question really was :

print string reverse AND with each next iteration shift begining of the string by 1 position....