954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String Method Question

I am writing a method that has to do with strings and I was stuck on how to go about coding this. Any help getting it started or tips at all would be great. Thanks.


MyString::MyString(const char* s)

This constructor takes a pointer to a constant C-style string as its argument. It should set the size of the string to the number of characters in the C-style string (not including the null character), allocate a string text array of that size, and then copy the characters of the C-style string into the string text array.

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

For getting the size, you can already use the strlen function:

size_t sz;
sz = strlen(s);


You can usenew[] and delete[] for the memory allocation to hold the desired string.

After that, you copy the string passed via the argument to the newly allocated memory, use strcpy.

Note: Within your object you'll do some dynamic memory allocation.
You'll probably want to implement a destructor, a copy constructor, and overload the assignment operator as well.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

You probably already know this but don't forget the +1 in the allocation for storing the terminator.

sz = strlen( s );
char *p = new char[ sz+1 ]
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

Ya i'll be writing a destructor, copy constructor, and overload methods as the program goes on. This is what I have came up with for this method I feel like the third like in the method is incorrect.

MyString::MyString(const char* s)
        {
        stringSize = strlen(s);
        new char[stringSize];
        strcpy (char[stringSize, s);
        }
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Corrections:

MyString::MyString(const char* s)
{
    <strong>// don't forget the type:</strong>
    <strong>size_t</strong> stringSize = strlen(s);

    <strong>// you need a pointer to the newly allocated memory:</strong>
    <strong>// don't forget to allocate memory for the null-terminator as well:</strong>
    <strong>char *p</strong> = new char[stringSize<strong>+1</strong>];

    strcpy (<strong>p</strong>, s);
}
tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

What Tux4Life said!

But would make sense to store the string size so future strlen() requests of your string in class only needs to return the value. Not look it up!

MyString::MyString(const char* s)
{
        stringSize = strlen(s);
        szString = new char[ stringSize + 1];
        strcpy ( szString, s );
}
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
 

For my stringSize I have the type declared as a char* in my header file so I would use that instead of size_t correct? Also the char* p I just create within the method right so it can hold the new array?

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

>For my stringSize I have the type declared as a char* in my header file so I would use that instead of size_t correct?
Nope, to store a size, you don't use a char pointer, instead you use size_t (you can compare it to an unsigned int).

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Awesome thanks for the help guys, got that working, might be back with a few questions later.

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Alright came across an issue with overloading this is the first time I have done any operator overloading so bare with me haha.

This is what it is suppose to do.

const MyString& operator=(const char* rightOp)

The assignment operator should also be overloaded to allow assignment of a C-style string to a MyString object.

This is what I got and I am getting quite a few errors.

const MyString& MyString::operator=(const char* rightOp)
        {
        if(this != *rightOp)
                {
                delete[] stringStorage;
                stringStorage = new char[rightOp.stringSize];
                for(int = 0; i < rightOp.stringSize; i++)
                        stringStorage[i] = rightOp.stringStorage[i];
                stringSize = rightOp.stringSize;
                }
        return *this;
        }
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Without listing the first few errors you receive it's not possible to say with surety what you have going. However, the object you have used called this is a pointer and rightOp is apparently a C styled string. As such you can't derefernce a C styled string. What you probable want is to see if the address stored in this and the address of rightOp are the same address (ie, are the same object).

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Sorry about that the errors I am getting are listed below. Thanks.

MyString.cpp: In member function âconst MyString& MyString::operator=(const char*)â:
MyString.cpp:59: error: ISO C++ forbids comparison between pointer and integer
MyString.cpp:62: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:63: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:64: error: request for member âstringStorageâ in ârightOpâ, which is of non-class type âconst char*â
MyString.cpp:65: error: request for member âstringSizeâ in ârightOpâ, which is of non-class type âconst char*â

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

Yup, that's what the first error message says to me. You could try something like this:

if(this != rightOp)

sinc the name of an array acts as an address for the first element of that array. Hopefully they will allow comparisons of two addresses. If that doesn't work, then someone else can probably give you the correct version.

Alternatively, since this will never be a simple C Style string it can never have the same address as a C Style string and therefore you could probably get by without the comparison. If you are assigning one MyString to another, however, then it could in theory be the same address, so looking for identity is recommended.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

I went ahead and got rid of the comparison because I was looking through notes and saw that in this case it is not needed. Still having issues with the other errors though. I'm going to keep playing with it though, I'll be on here if anyone has anymore advice. Thanks!

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

MyString::MyString(const char* s)

This constructor takes a pointer to a constant C-style string as its argument. It should set the size of the string to the number of characters in the C-style string (not including the null character), allocate a string text array of that size, and then copy the characters of the C-style string into the string text array.

I was told strcpy does not work for this method and that I for loop should be used. I get everything coded correctly except the for loop which has me a bit confused. Any suggestions?

MyString::MyString(const char* s)
        {
        size_t stringSize = strlen(s);
        *stringStorage = new char[stringSize+1];
        for(int i = 0; stringSize > stringStorage; i++)
                char[stringSize+1] = s;
        }
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

>It should set the size of the string to the number of characters in the C-style string (not including the null character), allocate a string text array of that size, and then copy the characters of the C-style string into the string text array.

Why would someone want to do that?
You have to allocate space for the null terminator as well.
Otherwise you'll run into problems while displaying the string using cout for example.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

I am certainly no expert in STL, but it is my understanding that the const char * embedded in the STL string class isn't necessarily implemented as a C style string----that is, it isn't necessarily a null terminated char array. To output such an object you could overload the << operator using a loop in the method body to display each char separately, just like you could use a loop rather than strcpy() to assign/copy the object. I suspect if you look under the hood of strcpy() you will find a loop as well, but we're even further out of my surety level on that speculaton. The only reason to do this that I can think of is to make the STL string object independent of any C coding requirements. Independent confirmation not a bad idea before spreading this around too far is a good idea.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

Well, as long as you hold the correct number of characters in a variable, this would be no problem, but a null-terminator takes 1 byte extra space, and you won't have to add several other routines to make the string display correctly on the screen.

But, of course, if his assignment is to do it without a null-terminator, then my advise probably doesn't make much sense :)

>The only reason to do this that I can think of is to make the STL string object independent of any C coding requirements.
Yeah, probably his teacher wants that he avoids functions from the standard function library.

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

Ya I don't know why he wants us to ignore the null terminator but he does. So I am at a standstill with this for now I guess. any idea how I could code this for loop? I cant really go any farther without it.

lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

These are the three files I have for this program so far I am also getting a segmentation fault error, so if anyone is willing to look at these that be great. I attached them to this post with the link to the assignment if needed. Also, assign3.cpp is given by my teacher so I did not have to do with the coding on that.

http://www.cs.niu.edu/~mcmahon/cs241/Assign/as3241m09.html

Attachments MyString.cpp (2.73KB) MyString.h (1.01KB) assign3.cpp (2.71KB)
lancevo3
Junior Poster
146 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You