Solved

writing a simple program(recursive)

Posted on 2002-06-26
10
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

615 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