Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating string using class, dynamic mem alloc for charac values HELP! 1

Status
Not open for further replies.

DonCL

Programmer
Jul 16, 2001
43
US
Hello,
i have to define a string and implement it using a class mystring and dynamic memory allocation for character values. i cannot use the #include <string> definition. It must have these methods:
create a string (obviously)
check if the string is empty
find a given substring in a string
calculate the length of a string
create a substring
read a string form input stream
and finaly, print the string
I also have to write a driver to check and demonstate.

Can someone please help me write the code for this type of program? I would appreciate any help... Thanks a lot!

GOD BLESS AMERICA
 
lets say i would like to use the string &quot;Hello, my name is DonCL&quot; ...
 
This is not a trival assignment. No wonder you did not get the response. I'll give you an image of this. But I did not test it. And you have to complete remaining member functions.

class String
{
public:
char * strstr(const char *aStr);
int GetLength();
bool IsEmpty();
String(char *);
String();
virtual ~String();

private:
char * _strBuf;
};

String::String()
{

}

String::~String()
{
if (_strBuf) delete [] _strBuf;
}

String::String(char * aStr)
{
int len =0;
do{
if (aStr[len] == '\0')break;
len++;
}while(1);
if (len){
_strBuf = new char[len+1];
for (int i=0;i<len;i++) _strBuf = aStr;
}

}

bool String::IsEmpty()
{
if (GetLength()) return false;
else return true;
}

int String::GetLength()
{
int i = 0;

do {
if (_strBuf == '\0') return i;
i++;
}while(1);

return 0;
}
// Find a substring
char * String::strstr(const char * aStr)
{
int j = 0;
int i = 0;
int start;
do {
if (_strBuf == aStr[j]) {
if (j==0) start = i;
j++;
if (aStr[j] == '\0') return (char *)&_strBuf[start];;
}else if (j!=0) j==0;
j++;

}while (j<GetLength());
return 0;
}
 
Well, just beaten to the post:) Here's another class which I think does all you've requested (it is tested!). I provide >> and << operators for cin and cout (but see below). Also I've used C library functions to save some time and trouble.

#include &quot;stdafx.h&quot;
#include <iostream>
using namespace std;

class str
{
char *m_pStr;
public:
str() {m_pStr = new char[1]; *m_pStr = 0;};
str(const char* pc) {m_pStr = new char[strlen(pc)+1]; strcpy(m_pStr,pc);}
~str() {delete [] m_pStr;}
bool IsEmpty() const {return strlen(m_pStr)==0;}
int Length() const {return strlen(m_pStr);}
char* Find(const char* s) const {return strstr(m_pStr,s);}
char* SubStr(const int start, const int length) const
{
if (start<0||length<1||strlen(m_pStr)<start+length) return NULL;
char* t = new char[length+1];
strncpy(t,m_pStr+start,length);
*(t+length) = 0;
return t;
};

const char* c_str() const {return m_pStr;}
friend istream& operator >> (istream& r, str& s);
};

ostream& operator << (ostream& r, const str& s) {r << s.c_str(); return r;}

/*
istream& operator >> (istream& r, str& s)
{
delete [] s.m_pStr;
char t[100];
r >> t;
s.m_pStr = new char[strlen(t)+1];
strcpy(s.m_pStr,t);
return r;
}
*/

int main(int argc, char* argv[])
{
str s1;
str s2(&quot;Some data&quot;);
const str cs(&quot;Help!&quot;);
cout << (s1.IsEmpty() ? &quot;s1 empty&quot; : &quot;s1 not empty&quot;) << endl;
cout << (s2.IsEmpty() ? &quot;s2 empty&quot; : &quot;s2 not empty&quot;) << endl;
cout << &quot;s1 length=&quot; << s1.Length() << endl;
cout << &quot;s2 length=&quot; << s2.Length() << endl;
cout << &quot;start of \&quot;data\&quot; in s2=&quot; << (s2.Find(&quot;data&quot;)- s2.c_str()) << endl;

cout << (s2.SubStr(0,0)==NULL) << endl; // Error NULL, result 1
cout << (s2.SubStr(s2.Length(),0)==NULL) << endl; // Error NULL, result 1
cout << (s2.SubStr(s2.Length()+1,0)==NULL) << endl; // Error NULL, result 1
cout << (s2.SubStr(-1,0)==NULL) << endl; // Error NULL, result 1
cout << &quot;>&quot; << s2.SubStr(3,3) << &quot;<&quot; << endl; // &quot;e d&quot; hopefully

cout << s2 << &quot;hey!&quot; << s2 << s2 << &quot;there!&quot; << endl;

// char pc[100];;
// cin >> s2 >> pc;
// cout << &quot;>&quot; << s2 << &quot;<&quot; << pc << endl;
return 0;
}

Unfortunately there's few bugs in the VC++ 6 compiler that prevent the istream (cin) function and the calls from compiling. This is all fixed in VC++ 7 (.Net beta2).

Firstly the friend declaration is not recognised correctly, causing the m_pStr member to be inaccessible. Simplest solution would be to declare it public as a workaround. Secondly the compiler considers the &quot;>>&quot; operator in

cin >> s2 >> pc;

to be ambiguous. I'm not sure how to get round this one. Frankly I've forgotten what sp level I have on my machine here at home; maybe it all works with sp5. You can uncomment the code and try it out.

I added the c_str() class function (just like <string>) so I could get the offset of the Find() result, although it's a useful function to have anyway. :) Hope that this helped! :)
 
Thanks for the responses... I will try them out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top