Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

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.
0
likempeachesnow
Asked:
likempeachesnow
  • 5
  • 3
  • 2
1 Solution
 
mblatCommented:
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....
0
 
likempeachesnowAuthor Commented:
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);
     }
}
0
 
mblatCommented:
Well in my opinion that was good first try....

How about this:

#include <iostream>
#include <string>
#include <algorithm>

#include <conio.h>

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

void stringReverse(char*,char);

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: ";
   
    for(x=0;x>printThis;++x)
         cout<<name[x]<<' ';
   
    cout<<endl;        

    getch();
    return 0;
}

void stringReverse(char* name,char start)
{
         //midpoint=splitList(name,start,stop);
         // I am not sure what is midpoint for?
        // you said print string backwards?

    std::string str = name + start;
    // reverse is part of Standard Template Library (STL) - somewhat complicated thing by itself - by easy in your case....
    std::reverse(str.begin(),str.end());

    cout << str << endl;

    // test for the end of string:
    // C and C++ strings are 0 teminated.
    // note that they are not the same with std::string which is also part of STL and VERY different animal...
    if(!name[start])
        return;

    stringReverse(name,start + 1);
}


Hope it helps...
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mblatCommented:
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;
}
0
 
likempeachesnowAuthor Commented:
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!!
0
 
CoolBreezeCommented:
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.
0
 
CoolBreezeCommented:
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...
0
 
mblatCommented:
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 :-)
0
 
CoolBreezeCommented:
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
0
 
mblatCommented:
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....
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now