Jump to content
  • Advertisement
Sign in to follow this  
ontheheap

Tutorial question: Making a String class in C++

This topic is 5410 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OK, so I'm finally learning how to use Classes in c++. I've been writing a String class which has the ability to reverse, append, and change the string to a different string. This is what I've come up with: Meh, I'll explain the problem and where the problem code is at before posting the code: in my main function, I'm using cout.write() to test the Class. For some reason strlen(newString) is returning 0 when I call it after my reverseString() function. Main() is the last code box below, and I put comments next to what I'm talking about.
class simpleString {

public:

	simpleString();
	simpleString(char* tehString);
	simpleString(const simpleString& aString);
	virtual ~simpleString();

	void printString();
	char* reverseString();
	char* appendString(char* toAppend);
	char* resizeString(char* newString);

private:
	char* theString;
};

simpleString::simpleString()
{
	theString = "hello";
}

simpleString::simpleString(char* tehString)
{
	theString = tehString;
}

simpleString::simpleString(const simpleString& aString)
{
	// not being used yet
}

simpleString::~simpleString()
{
	theString = NULL;
}

void simpleString::printString()
{
	cout << theString << endl;
}

char* simpleString::reverseString()
{
	int length = strlen(theString)+1;
	char* temp = new char[strlen(theString)];
	for(int i=0,j=length;i<length;i++,j--)
		temp = theString[j-1];
	return temp;
}


char* simpleString::appendString(char* toAppend)
{
	int tempLength = strlen(theString) + strlen(toAppend)+2;
	char* temp = new char[tempLength];
	strcpy(temp,theString);
	for(int i = strlen(theString),j=0;i<tempLength;i++,j++)
		temp = toAppend[j];
	return temp;
}

char* simpleString::resizeString(char* newString)
{
	theString = NULL;
	theString = new char[strlen(newString)+1];
	theString = newString;
	return theString;
}

int main()
{
	simpleString *testString = new simpleString();
	char* newString = NULL;

	cout << "printString()" << endl;
	testString->printString();
	cout << endl;

	newString = testString->reverseString();
	cout << "reverseString()" << endl;
	cout.write(newString,strlen(newString)); /* strlen is returning 0, however this works if I replace the strlen call with a number*/
	cout << endl;
	cout << endl;

	newString = testString->appendString(" world");
	cout << "appendString(char*)" << endl;
    cout.write(newString,strlen(newString)); /* this works fine*/
	cout << endl;
	cout << endl;

    newString = testString->resizeString("goodbye world");
	cout << "resizeString(char*)" << endl;
	cout.write(newString,strlen(newString));
	cout << endl;
	cout << endl;

	cout << "calling printString() again to test resizeString()" << endl;
	testString->printString();
	cout << endl;
	cout << endl;

	newString = testString->reverseString();
	cout << "calling reverse on the new string" << endl;
	cout.write(newString,strlen(newString));
	
	delete(testString);
	cout << "\n\n";
	return 0;
}

This is really my first experience using C++, so please go easy on me ;). - heap

Share this post


Link to post
Share on other sites
Advertisement
Quote:

in my main function, I'm using cout.write() to test the Class. For some reason strlen(newString) is returning 0 when I call it after my reverseString() function. Main() is the last code box below, and I put comments next to what I'm talking about.


Blind Shot:You are reversing and putting the null which is supposed at the end of the string at the beggining.

After Deep Look
I was right.The problem is where you are calling strlen() in the reverse string funtion.
Quote:

int length = strlen(theString)+1;

stlen() returns the real length of the string rather than the array index of the last item.So the string "ABC" is of length 3 and 'A' is at array[0], 'B' is at array[1] ,'C' is at array[2] and at array[3] is NULL or 0

Share this post


Link to post
Share on other sites
doh!

You're absolutely right. OK... so here I go again. I'll just skip the last array element in the original string, and then add it at the end of the new one.

I can't believe I didn't see that...

thanks FireNett

Edit:

I changed my reverseString() function to


char* simpleString::reverseString()
{
int length = strlen(theString)+1;
char* temp = new char[strlen(theString)];
for(int i=0,j=length-1;i<length;i++,j--)
{
temp = theString[j-1];
if(i == length-1)
temp = theString[length];
}
return temp;
}



and now it is working fine =)

- heap

Share this post


Link to post
Share on other sites
You are most welcome [totally].

A small improvement suggestion.Instead of adding one when you find the string length just let it be(look under After Deep .. of my first post for exp,you will understand).


Edit:adding the improved version ;)


char* simpleString::reverseString()
{
// 0 1 2 3
//array[A][C][NULL]
// LENGTH = 3

int length = strlen(theString);
char* temp = new char[strlen(theString)];

//Len - 1 = 2 => array[2] => 'C'
for(int i=0,j=length-1;i<length;i++,j--)
{
temp = theString[j]; //Take note
}
return temp;
}




Clears stuff?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!