View Single Post
  #6 (permalink)  
Old 10-04-2006, 08:15 PM
hoser2001's Avatar   
hoser2001 hoser2001 is offline
Programmer
 
Join Date: Jul 2006
Posts: 175
Credits: 0
Rep Power: 11
hoser2001 is on a distinguished road
Default

Alright, I finally got my class to compile, and I'm pretty sure everything is alright, but now I have to use this class in another program in which I perform a couple of recursive operations. RECURSION PWNS ME....

Code:
#include <cstdlib>
#include <iostream>

using namespace std;

class Cstring
{
private:
 //array that will hold the strings
 char array[50];
public: 
 //default constructor
 Cstring();   
 //constructor that turns a single character into a string  
 Cstring(char&);
 //constructor that creates a copy of a string
 Cstring(Cstring&);
 //////////////////////////////////
 //overloaded operators for strings
 //////////////////////////////////
 //this operator adds a single character to the end of a string
 friend Cstring operator + (Cstring left, const char right);
 //this operator stores a copy of a string in a result string
 Cstring Cstring::operator = (Cstring& right);
 //these are my input/output operators
 friend istream& operator >> (istream& input, Cstring& right);
 friend ostream& operator << (ostream& output, Cstring& right); 
 //function that returns the first character of the string   
 char head();
 //function that returns the string with the first character removed
 Cstring tail();
};

/////////////////////////////////////
//Constructors
////////////////////////////////////

//default constructor
Cstring::Cstring()
{
 for(int i=0; i<50; i++)
 array[i]= ' ';
}

//constructor that makes character a string
Cstring::Cstring(char &stuff)
{
 array[0] = stuff;
 for (int i=1; i<50; i++)
 array[i] = ' ';
}

//constructor that copies string to result string
Cstring::Cstring(Cstring &other)
{
 for (int i=0; i<50; i++)
 array[i] = other.array[i];
}

/////////////////////////////////////
//Functions
/////////////////////////////////////

//this is the head function that returns first character of string
char Cstring::head()
{
 return array[0];
}

//this is the tail function that returns the string without the first character
Cstring Cstring::tail()
{
 Cstring temp;
 for(int i=1; i<50; i++)
 {
  temp.array[i] = array[i];     
 }
 return temp;  
}

///////////////////////////////////
//Operator Overloads
///////////////////////////////////

Cstring operator+ (Cstring left, char right)
{
 Cstring temp;
 for(int i=0; i<50; i++)
   temp.array[i] = left.array[i];
         
 for(int j=0; j<50; j++)
 {
  if(temp.array[j]==' ')
  {
   temp.array[j] = right;
   break;
  }
 }
 return temp;           
}

ostream& operator<<(ostream& output, Cstring &right)
{
 for(int i=0; i<50; i++)
 {
  if(right.array[i] != ' ')
  output << right.array[i];
 }
}

istream& operator>>(istream& input, Cstring &right)
{
 Cstring temp;
 input.getline(temp.array,50);
 input >> temp;
}
Cstring Cstring::operator=(Cstring& right)
{
 int i=0;
 Cstring temp;
 temp.array[i] = right.array[i];
 return temp;
}
I have 3 recursive operations I need to perform:
1.)a function that returns the reverse of a string
2.)a function that concatenates the reverse of a string onto the original string
3.)a function that tests if a certain string is the prefix of another string

If anyone can point me in some possible dirrection, or even try to explain to me exactly how it is recursion works I would appreciate the help greatly!!!!
Reply With Quote