I have a program where I make my own string class using c-strings. I have the pr
ID: 3915782 • Letter: I
Question
I have a program where I make my own string class using c-strings. I have the program but, I always have segmentation faults. I just need help with fixing my program.
My .h file
mystring.h
the .h file can't be changed
#ifndef MYSTRING_H
#define MYSTRING_H
#include
using namespace std;
class MyString
{
friend ostream& operator<< (ostream& , const MyString& );
friend istream& operator>> (istream& , MyString& );
friend istream& getline (istream& , MyString& , char delim = ' ');
friend MyString operator+ (const MyString& , const MyString& );
friend bool operator< (const MyString& , const MyString& );
friend bool operator> (const MyString& , const MyString& );
friend bool operator<=(const MyString& , const MyString& );
friend bool operator>=(const MyString& , const MyString& );
friend bool operator==(const MyString& , const MyString& );
friend bool operator!=(const MyString& , const MyString& );
public:
MyString(); // empty string
MyString(const char* ); // conversion from c-string
MyString(int ); // conversion from int
~MyString(); // destructor
MyString(const MyString& ); // copy constructor
MyString& operator=(const MyString& ); // assignment operator
MyString& operator+=(const MyString& ); // concatenation/assignment
// bracket operators to access char positions
char& operator[] (unsigned int index);
const char& operator[] (unsigned int index) const;
// insert s into the string at position "index"
MyString& insert(unsigned int index, const MyString& s);
// find index of the first occurrence of s inside the string
// return the index, or -1 if not found
int indexOf(const MyString& s) const;
int getLength() const; // return string length
const char* getCString() const; // return c-string equiv
MyString substring(unsigned int , unsigned int ) const;
MyString substring(unsigned int ) const;
private:
char * str;
int size;
};
#endif
mystring.cpp
#include
#include
#include
#include
#include "mystring.h"
MyString::MyString()
{
size = 0;
str = NULL;
}
MyString::MyString(const char* s)
{
size = strlen(s);
str = new char[size+1];
strcpy(str,s);
}
MyString::MyString(int convert)
{
int con = convert;
int counter = 0;
do
{
con = con / 10;
counter++;
}
while(con != 0);
size = counter + 1;
str = new char[size];
do
{
str[counter - 1] = char((convert % 10) + 48);
convert = convert / 10;
counter--;
}
while(counter >= 1);
}
MyString::~MyString()
{
delete [] str;
}
MyString::MyString(const MyString& second)
{
size = second.size;
str = new char[size+1];
strcpy(str,second.str);
}
MyString& MyString::operator=(const MyString& second )
{
if (this != &second)
{
delete []str;
size = strlen(second.str);
str = new char[size+1];
strcpy(str,second.str);
}
return *this;
}
MyString& MyString::operator+=(const MyString& first)
{
size = size + first.size;
char *temp = str;
str = new char[size+1];
strcat(str,first.str);
return *this;
}
ostream& operator<< (ostream& os, const MyString& s)
{
// a regular os << does not seem to work so I tried using a for loop
// and put
for (int i = 0; i
os.put(s.str[i]);
return os;
}
istream& operator>> (istream& is, MyString& s)
{
char c;
s = " ";
while (is && isspace(is.peek()))
is.ignore();
while (is && !isspace(is.peek()))
{
is >> c;
s += c;
}
return is;
}
istream& getline (istream& is, MyString& s, char delim)
{
MyString string;
char cha;
is.get(cha);
while (cha!= ' ' && !is.eof())
{
string+=cha;
is.get(cha);
}
s=string;
return is;
}
MyString operator+ (const MyString& first, const MyString& second)
{
return strcat(first.str,second.str);
}
char& MyString::operator[] (unsigned int index)
{
return str[index];
}
const char& MyString::operator[] (unsigned int index) const
{
return str[index];
}
MyString& MyString::insert(unsigned int index, const MyString& s)
{
char* string = new char[size + s.size];
strcpy(string,str);
for(int i = 0; i < s.size; i++)
string[i + index] = s.str[i];
for(int i = index + s.size; i < size + s.size -2; i++)
string[i] = str[i - s.size +1];
delete [] str;
str = string;
size= size + s.size;
return *this;
}
int MyString::indexOf(const MyString& s) const
{
if(size < s.size)
return -1;
else if(size == s.size)
{
int index = 0;
while((str[index] == s.str[index]) && (str[index] != ''))
index++;
if(str[index] == '')
return 0;
else
return -1;
}
else
{
int index2 = 0;
while(index2 < (size - s.size + 1))
{
int j = 0;
int k = 0;
while((s[0] != str[index2]) && (index2 < (size - s.size + 1)))
index2++;
if(index2 == size - s.size + 1)
return -1;
else
{
k = index2;
while((s[j] == str[k]) && (s[j] != ''))
{
k++;
j++;
}
if(s[j] == '')
return index2;
else
index2++;
}
}
}
}
int MyString::getLength() const
{
return size;
}
const char* MyString::getCString() const
{
return str;
}
MyString MyString::substring(unsigned int index, unsigned int sz ) const
{
MyString sub = substring(index);
if(sub.size - 1 <= sz)
{
return sub;
}
else
{
char* tempstring = new char[sz + 1];
for(int i = 0; i < sz; i++)
{
tempstring[i] = sub.str[i];
}
tempstring[sz] = '';
delete [] sub.str;
sub.str = tempstring;
sub.size = sz + 1;
return sub;
}
}
MyString MyString::substring(unsigned int index) const
{
MyString sub;
int tempsize = size - index;
if(tempsize <= 1)
{
sub.str = '';
sub.size = 1;
}
else
{
char* tempstring = new char[tempsize];
for(int i = 0; i < tempsize - 1; i++)
{
tempstring[i] = str[i + index];
}
tempstring[tempsize - 1] = '';
delete [] sub.str;
sub.str = tempstring;
sub.size = tempsize;
}
return sub;
}
bool operator< (const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)<0;
}
bool operator> (const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)>0;
}
bool operator<=(const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)<=0;
}
bool operator>=(const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)>=0;
}
bool operator==(const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)==0;
}
bool operator!=(const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)!=0;
}
driver.cpp
#include
using namespace std;
#include "mystring.h"
int main()
{
MyString s1;
MyString s2("Hello, World");
MyString s3 = "Welcome to Florida, have a nice day";
MyString s4 = 12345;
cout << "s1 = " << s1 << ' ';
cout << "s2 = " << s2 << ' ';
cout << "s3 = " << s3 << ' ';
cout << "s4 = " << s4 << ' ';
cout << ' ';
cout << "Making the calls: "
<< " cin >> s1 "
<< " getline(cin, s2, ',') "
<< " getline(cin, s3) ";
cout << "Enter some sentences: ";
cin >> s1;
getline(cin,s2,',');
getline(cin,s3);
cout << " New string values: ";
cout << "s1 = " << s1 << ' ';
cout << "s2 = " << s2 << ' ';
cout << "s3 = " << s3 << ' ';
cout << "--------------------------- ";
// ----------------------------------
s1 = "Dog";
s2 = "Food";
MyString result = s1 + s2;
cout << "result = " << result << ' ';
s1 += s2;
cout << "s1 = " << s1 << endl;
const MyString s5 = "The concatenation of the catapult is a catamaran";
cout << "s5 = " << s5 << endl;
cout << "s5.indexOf("cat") returns " << s5.indexOf("cat") << ' ';
cout << "s5.indexOf("dog") returns " << s5.indexOf("dog") << ' ';
cout << "s5.getLength() = " << s5.getLength() << ' ';
cout << "s5[4] = " << s5[4] << ' ';
cout << "s5[10] = " << s5[10] << ' ';
cout << "s5[15] = " << s5[15] << ' ';
cout << "s5[52] = ascii " << static_cast(s5[52]) << ' ';
cout << "s5.substring(10,16) = " << s5.substring(10,16) << ' ';
cout << "s5.substring(23) = " << s5.substring(23) << ' ';
cout << "----------------------------- ";
MyString words = "Greetings, Earthling";
cout << "words = " << words << ' ';
cout << "words.getLength() = " << words.getLength() << ' ';
words[0] = 'K';
words[4] = 'p';
words[16] = 'z';
cout << "words = " << words << ' ';
words[25] = 'Q';
cout << "words = " << words << ' ';
words.insert(11, "Insane ");
cout << "words = " << words << ' ';
cout << "----------------------------- ";
MyString x = "apple", y = "apply";
cout << "x = " << x << ' ';
cout << "y = " << y << ' ';
if (x < y) cout << "x < y is true ";
if (x > y) cout << "x > y is true ";
if (x <= y) cout << "x <= y is true ";
if (x >= y) cout << "x >= y is true ";
if (x == y) cout << "x == y is true ";
if (x != y) cout << "x != y is true ";
}
Explanation / Answer
Hi, please go threw code and comments. I edited some parts. please go threw it. and also go threw output.
mystring.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include<iostream>
using namespace std;
class MyString
{
friend ostream& operator<< (ostream& , const MyString& );
friend istream& operator>> (istream& , MyString& );
friend istream& getline (istream& , MyString& , char delim = ' ');
friend MyString operator+ (const MyString& , const MyString& );
friend bool operator< (const MyString& , const MyString& );
friend bool operator> (const MyString& , const MyString& );
friend bool operator<=(const MyString& , const MyString& );
friend bool operator>=(const MyString& , const MyString& );
friend bool operator==(const MyString& , const MyString& );
friend bool operator!=(const MyString& , const MyString& );
public:
MyString(); // empty string
MyString(const char* ); // conversion from c-string
MyString(int ); // conversion from int
~MyString(); // destructor
MyString(const MyString& ); // copy constructor
MyString& operator=(const MyString& ); // assignment operator
MyString& operator+=(const MyString& ); // concatenation/assignment
// bracket operators to access char positions
char& operator[] (unsigned int index);
const char& operator[] (unsigned int index) const;
// insert s into the string at position "index"
MyString& insert(unsigned int index, const MyString& s);
// find index of the first occurrence of s inside the string
// return the index, or -1 if not found
int indexOf(const MyString& s) const;
int getLength() const; // return string length
const char* getCString() const; // return c-string equiv
MyString substring(unsigned int , unsigned int ) const;
MyString substring(unsigned int ) const;
char * str; // str should be private then only you can direcly access int other function . and it must.
private:
int size;
};
#endif
mystring.cpp
#include<iostream>
#include <string.h>
#include <fstream>
//#include
#include "mystring.h"
MyString::MyString()
{
size = 0;
str =NULL;
}
MyString::MyString(const char* s)
{
size = strlen(s);
str = new char[size+1];
strcpy(str,s);
}
MyString::MyString(int convert)
{
int con = convert;
int counter = 0;
do
{
con = con / 10;
counter++;
}
while(con != 0);
size = counter + 1;
str = new char[size];
do
{
str[counter - 1] = char((convert % 10) + 48);
convert = convert / 10;
counter--;
}
while(counter >= 1);
}
MyString::~MyString()
{
if(size != 0)
delete [] str;
}
MyString::MyString(const MyString& second)
{
size = second.size;
str = new char[size+1];
strcpy(str,second.str);
}
MyString& MyString::operator=(const MyString& second )
{
if (this != &second)
{
delete []str;
size = strlen(second.str);
str = new char[size+1];
strcpy(str,second.str);
}
return *this;
}
MyString& MyString::operator+=(const MyString& first)
{
size = size + first.size;
char *temp = str;
str = new char[size+1];
strcat(str,first.str);
return *this;
}
ostream& operator<< (ostream& os, const MyString& s)
{
// a regular os << does not seem to work so I tried using a for loop
// and put
for (int i = 0; i< s.size; i++)
{
os.put(s.str[i]);
return os;
}
}
istream& operator>> (istream& is, MyString& s)
{
char c;
s=" ";
while (is && isspace(is.peek()))
is.ignore();
while (is && !isspace(is.peek()))
{
is >> c;
s += c;
}
return is;
}
istream& getline (istream& is, MyString& s, char delim)
{
MyString string;
char cha;
is.get(cha);
while (cha!= ' ' && !is.eof())
{
string+=cha;
is.get(cha);
}
s=string;
return is;
}
MyString operator+ (const MyString& first, const MyString& second)
{
return strcat(first.str,second.str);
}
char& MyString::operator[] (unsigned int index)
{
return str[index];
}
const char& MyString::operator[] (unsigned int index) const
{
return str[index];
}
MyString& MyString::insert(unsigned int index, const MyString& s)
{
char* string = new char[size + s.size];
strcpy(string,str);
for(int i = 0; i < s.size; i++)
string[i + index] = s.str[i];
for(int i = index + s.size; i < size + s.size -2; i++)
string[i] = str[i - s.size +1];
delete [] str;
str = string;
size= size + s.size;
return *this;
}
int MyString::indexOf(const MyString& s) const
{
if(size < s.size)
return -1;
else if(size == s.size)
{
int index = 0;
while((str[index] == s.str[index]) && (str[index] != ''))
index++;
if(str[index] == '')
return 0;
else
return -1;
}
else
{
int index2 = 0;
while(index2 < (size - s.size + 1))
{
int j = 0;
int k = 0;
while((s[0] != str[index2]) && (index2 < (size - s.size + 1)))
index2++;
if(index2 == size - s.size + 1)
return -1;
else
{
k = index2;
while((s[j] == str[k]) && (s[j] != ''))
{
k++;
j++;
}
if(s[j] == '')
return index2;
else
index2++;
}
}
}
}
int MyString::getLength() const
{
return size;
}
const char* MyString::getCString() const
{
return str;
}
MyString MyString::substring(unsigned int index, unsigned int sz ) const
{
MyString sub = substring(index);
if(sub.size - 1 <= sz)
{
return sub;
}
else
{
char* tempstring = new char[sz + 1];
for(int i = 0; i < sz; i++)
{
tempstring[i] = sub.str[i];
}
tempstring[sz] = '';
delete [] sub.str;
sub.str = tempstring;
sub.size = sz + 1;
return sub;
}
}
MyString MyString::substring(unsigned int index) const
{
MyString sub;
int tempsize = size - index;
if(tempsize <= 1)
{
sub.str = '';
sub.size = 1;
}
else
{
char* tempstring = new char[tempsize];
for(int i = 0; i < tempsize - 1; i++)
{
tempstring[i] = str[i + index];
}
tempstring[tempsize - 1] = '';
delete [] sub.str;
sub.str = tempstring;
sub.size = tempsize;
}
return sub;
}
bool operator< (const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)<0;
}
bool operator> (const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)>0;
}
bool operator<=(const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)<=0;
}
bool operator>=(const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)>=0;
}
bool operator==(const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)==0;
}
bool operator!=(const MyString& first, const MyString& second)
{
return strcmp(first.str,second.str)!=0;
}
driver.cpp
#include <iostream>
#include <fstream>
using namespace std;
#include "mystring.h"
int main()
{
MyString s1;
MyString s2("Hello, World");
MyString s3 = "Welcome to Florida, have a nice day";
MyString s4 = 12345;
std::cout << "s1 = " << s1 ;// << ' '; //we are printing newline after null so that it givs seg fault.
std::cout << "s2 = " << s2 << ' ';
std::cout << "s3 = " << s3 << ' ';
std::cout << "s4 = " << s4 << ' ';
cout << "Making the calls: "
<< " cin >> s1 "
<< " getline(cin, s2, ',') "
<< " getline(cin, s3) ";
cout << "Enter some sentences: ";
cin >> s1;
cout << "befor get line1" << endl;
cin.getline(s2.str,1000);
cout << "after get line1" << endl;
cin.getline(s3.str,1000);
cout << "after get line2" << endl;
cout << " New string values: ";
cout << "s1 = " << s1 << ' ';
cout << "s2 = " << s2 << ' ';
cout << "s3 = " << s3 << ' ';
cout << "--------------------------- ";
// ----------------------------------
s1 = "Dog";
s2 = "Food";
MyString result = s1 + s2;
cout << "result = " << result << ' ';
s1 += s2;
cout << "s1 = " << s1 << endl;
const MyString s5 = "The concatenation of the catapult is a catamaran";
cout << "s5 = " << s5 << endl;
cout << "s5.indexOf("cat") returns " << s5.indexOf("cat") << ' ';
cout << "s5.indexOf("dog") returns " << s5.indexOf("dog") << ' ';
cout << "s5.getLength() = " << s5.getLength() << ' ';
cout << "s5[4] = " << s5[4] << ' ';
cout << "s5[10] = " << s5[10] << ' ';
cout << "s5[15] = " << s5[15] << ' ';
cout << "s5[52] = ascii " << static_cast<MyString>(s5[52]) << ' ';
cout << "s5.substring(10,16) = " << s5.substring(10,16) << ' ';
cout << "s5.substring(23) = " << s5.substring(23) << ' ';
cout << "----------------------------- ";
MyString words = "Greetings, Earthling";
cout << "words = " << words << ' ';
cout << "words.getLength() = " << words.getLength() << ' ';
words[0] = 'K';
words[4] = 'p';
words[16] = 'z';
cout << "words = " << words << ' ';
words[25] = 'Q';
cout << "words = " << words << ' ';
words.insert(11, "Insane ");
cout << "words = " << words << ' ';
cout << "----------------------------- ";
MyString x = "apple", y = "apply";
cout << "x = " << x << ' ';
cout << "y = " << y << ' ';
if (x < y) cout << "x < y is true ";
if (x > y) cout << "x > y is true ";
if (x <= y) cout << "x <= y is true ";
if (x >= y) cout << "x >= y is true ";
if (x == y) cout << "x == y is true ";
if (x != y) cout << "x != y is true ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.