I have a program where I make my own string class using c-strings. I have the pr
ID: 3916203 • 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. In this program I cant change mystring.h or add #include<string>. This is a past assignment where I did not get a good grade. I would like to see the correct way of doing it.
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
I have fixed most of the code.... I could not get the getline(cin, s3) in the main() to work. It is unable to pass default parameter value of ' '. So I commented only that line in main() and ran the code. The program works without crashing... Hope it helps. The main point is that you should not forget to append '' at the end of strings... otherwise it will lead to segmentation faults.
Please do rate the answer if it helped. Thank you
mystring.cpp
-------
#include <iostream>
#include <cstring>
#include "mystring.h"
MyString::MyString()
{
size = 0;
str = new char[1];
str[0] = '';
}
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];
str[counter--] = '';
do
{
str[counter--] = char((convert % 10) + 48);
convert = convert / 10;
}
while(counter >= 0);
}
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];
strcpy(str, temp);
strcat(str,first.str);
delete []temp;
return *this;
}
ostream& operator<< (ostream& os, const MyString& s)
{
os << s.getCString() << endl;
return os;
}
istream& operator>> (istream& is, MyString& s)
{
char *c = new char[2];
c[1] = '';
s = "";
while (is && isspace(is.peek()))
is.ignore();
while (is && !isspace(is.peek()))
{
is >> c[0];
s += c;
}
delete[] c;
return is;
}
istream& getline (istream& is, MyString& s, char delim = ' ')
{
MyString string;
char *cha = new char[2];
cha[1] = '';
is.get(cha[0]);
while (cha[0] != delim && !is.eof())
{
string+=cha;
is.get(cha[0]);
}
s=string;
delete[] cha;
return is;
}
MyString operator+ (const MyString& first, const MyString& second)
{
MyString temp = first;
return temp+= second;
}
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)
{
if(index < size){
char* string = new char[size + s.size +1];
int i;
for(i = 0; i < index; i++)
string[i] = str[i];
for(int j = 0; j < s.size; j++)
string[i++] = s.str[j];
for(int j = index; j < size; j++)
string[i++] = str[j];
string[i] = '';
delete [] str;
str = string;
size= size + s.size;
}
return *this;
}
int MyString::indexOf(const MyString& s) const
{
if(size < s.size)
return -1;
else
{
int index = 0;
for(int i = 0; i <= size - s.size; i++)
{
bool found = true;
for(int j =0; j < s.size; j++)
{
if(s.str[j] != str[i+j])
{
found = false;
break;
}
}
if(found)
return i;
}
return -1;
}
}
int MyString::getLength() const
{
return size;
}
const char* MyString::getCString() const
{
return str;
}
MyString MyString::substring(unsigned int index, unsigned int sz ) const
{
if(index >= size)
return "";
if(index + sz > size)
sz = size - index;
char *temp = new char[sz+1];
for(int i =0; i < sz;i++)
temp[i] = str[index+i];
temp[sz] = '';
return MyString(temp);
}
MyString MyString::substring(unsigned int index) const
{
return substring(index, size-index);
}
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;
}
output
-----
s1 =
s2 = Hello, World
s3 = Welcome to Florida, have a nice day
s4 = 12345
Making the calls:
cin >> s1
getline(cin, s2, ',')
getline(cin, s3)
Enter some sentences: hi there how are you, come here
New string values:
s1 = hi
s2 = there how are you
s3 = Welcome to Florida, have a nice day
---------------------------
result = DogFood
s1 = DogFood
s5 = The concatenation of the catapult is a catamaran
s5.indexOf("cat") returns 7
s5.indexOf("dog") returns -1
s5.getLength() = 48
s5[4] = c
s5[10] = e
s5[15] = o
s5[52] = ascii r
s5.substring(10,16) = enation of the c
s5.substring(23) = e catapult is a catamaran
-----------------------------
words = Greetings, Earthling
words.getLength() = 20
words = Kreepings, Earthzing
words = Kreepings, Earthzing
words = Kreepings, Insane Earthzing
-----------------------------
x = apple
y = apply
x < y is true
x <= y is true
x != y is true
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.