Solved

writing a simple program(recursive)

Posted on 2002-06-26
10
194 Views
Last Modified: 2010-04-01
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
Comment
Question by:likempeachesnow
  • 5
  • 3
  • 2
10 Comments
 
LVL 4

Expert Comment

by:mblat
ID: 7112462
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
 

Author Comment

by:likempeachesnow
ID: 7112509
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
 
LVL 4

Accepted Solution

by:
mblat earned 50 total points
ID: 7112536
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
 
LVL 4

Expert Comment

by:mblat
ID: 7112540
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
 

Author Comment

by:likempeachesnow
ID: 7112679
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

by:CoolBreeze
ID: 7112689
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
 
LVL 3

Expert Comment

by:CoolBreeze
ID: 7112729
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
 
LVL 4

Expert Comment

by:mblat
ID: 7112774
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
 
LVL 3

Expert Comment

by:CoolBreeze
ID: 7112796
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
 
LVL 4

Expert Comment

by:mblat
ID: 7114009
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 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

11 Experts available now in Live!

Get 1:1 Help Now