Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

A custom String Class

string

  • Please log in to reply
15 replies to this topic

#1 hoser2001

hoser2001

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 170 posts

Posted 03 October 2006 - 05:00 PM

My entire programming experience has been based in C#, so my skills dealing with creating classes is somewhat limited, especially in C++.

I have to create a really simple string class that just does a few operations
(head and tail functions, concatenation operator, assignment operator and the input and output operators >> <<)

I also need a default constructor, and two other constructors for handling single characters and copying strings.

I want to be able to include it at the top of a program.

If anyone has any suggestions as to the best way to approach creating a class with just these simple requirements I would greatly appreciate any help.

Thanks in advance!
  • 0

#2 hoser2001

hoser2001

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 170 posts

Posted 04 October 2006 - 02:59 AM

Alright, I got my class put together, but I have an insane amount of compiler errors, and I don't get why most of them are there. I mostly followed examples online but for the most part I think it's right. I'm sure some of the errors are easily fixed, however I know I am missing a few major concepts here!
#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 characters to the end of a string
 friend Cstring operator + (Cstring&);
//this operator copies a string into a result string
 friend Cstring operator = (Cstring&);
//these are of course the input and output operators
 friend Cstring operator >> (Cstring&);
 friend Cstring operator << (Cstring&); 
 //function that returns the first character of the string   
 char head();
 //function that returns the string with the first character removed
 Cstring tail();
};

char Cstring::head()
{
 return array[0];
}

Cstring Cstring::tail()
{
 Cstring temp;
 for(int i=1; i<50; i++;)
 {
  temp.array[i] = array[i];     
 }
 return temp;  
}

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

Cstring::Cstring(char &this)
{
 array[0] = this;
 for (int i=1; i<50; i++;)
 array[i] = ' ';
}

Cstring::Cstring(Cstring &other)
{
 for (int i=0; i<50; i++;)
 array[i] = other.array[i];
}

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

Cstring::operator=(Cstring &left, Cstring &right)
{
 Cstring temp;
 temp.array[i] = right.array[i];
}

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);
 return temp;
}


  • 0

#3 Void

Void

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 403 posts

Posted 04 October 2006 - 05:04 AM

Hey, what compiler are you using?
  • 0
Void

#4 hoser2001

hoser2001

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 170 posts

Posted 04 October 2006 - 07:00 AM

bloodshed dev c++ 4.9.9.1
  • 0

#5 hoser2001

hoser2001

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 170 posts

Posted 04 October 2006 - 09:22 AM

Ok, I started over and decided to compile little bit at a time instead of cranking stuff out and trying to debug the whole thing... This is how far Ive gotten so far. I commented out the overloading of the = sign because I have no idea whats wrong with that, and I left in the main function because I dont know how to compile it without it, however the rest of it works until I try adding the method for overloading the + operator at which time I get this error message: "..... must take either zero or one argument"
#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&);
 //this operator stores a copy of a string in a result string
 //friend Cstring operator = (Cstring&); //wtf is wrong with this????
 //these are my input/output operators
 friend Cstring operator >> (istream&, Cstring&);
 friend Cstring operator << (ostream&, Cstring&); 
 //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 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;           
}





int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}


  • 0

#6 hoser2001

hoser2001

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 170 posts

Posted 04 October 2006 - 04:15 PM

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

#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!!!!
  • 0

#7 Ronin

Ronin

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 302 posts

Posted 05 October 2006 - 05:10 AM

That is a neat function. I'm not sure I will get the syntax right here but couldn't you use this to return the reverse of a string: (I'm also not sure of the array functions)

array[0] = array;
rArray[0] = array;
int count=0;

for (int i=length(array); i>=0; i--) 
{
     rArray[count] = array[i];
     count++;
}

I could be way off though.
  • 0

#8 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 05 October 2006 - 08:56 AM

You have most of what you need for doing a reverse.
The basic idea of recursion is to break a task into many smaller steps that are identical. For instance, if you want to compute a factorial (5! = 5*4*3*2*1), one way to do it is to say n! = n*(n-1)!, and note that 0!=1.

For reversing your string, just note that MyString.rev() = MyString.tail().rev()+MyString.head(), and a single character string is its own reverse.

Once you have part 1 done, part 2 is trivial. For testing the prefix, just test the heads and tails of the strings.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#9 Crane

Crane

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 388 posts

Posted 06 October 2006 - 10:37 AM

that is a nice looking class. May I ask why you are recreating a string class though?
  • 0

#10 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 07 October 2006 - 01:31 PM

It's a pretty standard programming project for classes.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#11 hoser2001

hoser2001

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 170 posts

Posted 08 October 2006 - 12:29 PM

Thanks for the explanation WingedPanther, however I seem to be having difficulties constructing a function like that.

Cstring reverse(Cstring string)
{
return reverse(string.tail()) + string.head()
}

if I do this and create a program that calls the reverse function the console closes when it gets to that line, I know this cause if I comment it out it stays open. Anyone know whats up?
  • 0

#12 hoser2001

hoser2001

    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 170 posts

Posted 08 October 2006 - 01:28 PM

Ok I got everying to work ...almost.

When I cout the reverse of the string I get the reverse of the string, but I also got a bunch of ** in memory before it.. so....

Cstring string = "cheese";
reverse(string) = crapinmemoryeseehc

I cant figure out where in my class the memory leak is coming from.

Here is the latest version of my 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 stuff);
//constructor that creates a copy of a string
 Cstring(const Cstring& other);
 
//function that returns the first character of the string   
 char head();
//function that returns the string with the first character removed
 Cstring tail();
 //function that returns length of string
 int length();
 
//////////////////////////////////
//overloaded operators for strings
//////////////////////////////////
 
//this operator adds a single character to the end of a string
 friend Cstring operator+ (const Cstring& left, const char right);
//this operator stores a copy of a string in a result string
 Cstring Cstring::operator= (const Cstring& right);
//these are my input/output operators
 friend istream& operator>> (istream& input, Cstring& right);
 friend ostream& operator<< (ostream& output, Cstring right);
};

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

//default constructor
Cstring::Cstring()
{
sprintf(array,"");
}

//constructor that makes character a string
Cstring::Cstring(char stuff)
{
 sprintf(array, "%c", stuff);
}

//constructor that copies string to result string
Cstring::Cstring(const Cstring &other)
{
 sprintf(array, "%s", other.array);
}

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

//this function is here so I can supply an ending condition to recursion
int Cstring::length()
{
 return strlen(array);   
}

//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<strlen(array)+1; i++)
 {
  temp.array[i-1] = array[i];     
 }
 return temp;  
}

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

Cstring operator+ (const Cstring& left, const char right)
{
 Cstring temp;
 for(int i=0; i<strlen(left.array)+1; i++)
   temp.array[i] = left.array[i];
         
 for(int j=0; j<strlen(temp.array)+1; j++)
 {
  if(temp.array[j]=='\0')
  {
   temp.array[j] = right;
   temp.array[j+1] = '\0';
   break;
  }
 }
 //cout << temp << endl;
 return temp;          
}

ostream& operator<<(ostream& output, Cstring right)
{
        Cstring temp;
        int length = strlen(right.array);
 for(int i=0; i<length; i++)
 {
  output << right.array[i];
 }
 return output;
}

istream& operator>>(istream& input, Cstring& right)
{
 input.getline(right.array, 50);
 return input;
}
Cstring Cstring::operator=(const Cstring& right)
{
 int i=0;
 Cstring temp;
 temp.array[i] = right.array[i];
 return temp;
}


  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download