Provide the denition of the following class that seeks to implement the STL stri
ID: 658142 • Letter: P
Question
Provide the denition of the following class that seeks to implement the STL string library. The testfunction and the expected output are provided.
class MyString {
public:
MyString();
MyString(const char* c_string);
MyString(const MyString& in_str);
~MyString();
int length() const; // Returns length of string
void clear(); // Erases the contents of the string, which becomes an empty string
/*
0 They compare equal
<0 Either the value of the first character that does not match is lower in the
compared string, or all compared characters match but the compared string is shorter.
>0 Either the value of the first character that does not match is greater in the
compared string, or all compared characters match but the compared string is longer.
*/
//Compare starting from index unpto n characters
int compare(int index, int n, const MyString& in_str)const;
//Returns the position of the first occurence of ch.
// Return -1 on failure
int find(char ch) const;
// Returns the position of the first occurence of substring starting at index.
// Returns -1 on failure.
int find(const MyString& in_str, int index) const;
// Outputs the string
friend ostream& operator <<(ostream& os, const MyString& in_str);
// Concantenates two strings
friend const MyString operator+(MyString& in_str1, MyString& in_str2);
private:
char* s;
};
int main()
{
const char* in_string = "Hello World!";
MyString str(in_string);
cout << str << endl; // Prints Hello World!
cout << str.length() << endl; // Prints 12
str.clear();
cout << str.length() << endl; // Prints a blank
if (str.empty())
cout << "Empty" << endl; // Prints empty
else
cout << "Not empty" << endl;
MyString str1("Howdy"), str2("HelloHowdy");
cout << str1 << " " << str2 << endl; // Prints Howdy HelloHowdy
cout << str2.compare(5,5,str1) << endl; // Prints 0
cout << str1.find('t') << endl; // Prints -1
cout << str2.find(str1,2) << endl; // Prints 5
MyString str3 = str2+str1;
cout << str3 << endl; // Prints HelloHowdyHowdy
MyString str4(str1);
cout << str4 << endl; // Prints Howdy
return 0;
}
2. Write the body of the following function: IntNodePtr splitList(IntNodePtr head, int element) that splits the input singly linked list into two. The second list starts from the node following the element. The function returns the head of the second list. If the element does not exist in the list a NULL pointer is returned.
class IntNode
{
public:
IntNode() {}
IntNode (int theData, IntNode* theLink) : data (theData), link(theLink){}
IntNode* getLink() const { return link;}
int getData() const {return data;}
void setData()(int theData) {data = theData;}
void setLink(IntNode* pointer){link = pointer;}
private:
int data;
IntNode* link;
};
typedef IntNode* LinkedIntNodePtr;
Explanation / Answer
Program code:
// Strings-UsingStringLibrary.cpp : Defines the entry point for the console application.
//Header files
#include "stdafx.h"
#include <iostream>
#include <string>
#include <string.h>
#include <cstring>
using namespace std;
class MyString
{
public:
//empty constructor
MyString()
{
s="";
}
//parameterized constructor
MyString( char* c_string)
{
s=c_string;
}
//overload constructor
MyString( MyString& in_str)
{
s=in_str.s;
}
//destructor
~MyString()
{
delete s;
}
// Returns length of string
int length()
{
return strlen(s);
}
// Erases the contents of the string, which becomes an empty string
void clear()
{
s="";
}
/*
0 They compare equal
<0 Either the value of the first character that does not match is lower in the
compared string, or all compared characters match but the compared string is shorter.
>0 Either the value of the first character that does not match is greater in the
compared string, or all compared characters match but the compared string is longer.
*/
//Compare starting from index upto n characters
int compare(int index, int n, MyString& in_str)
{
int j=0;
bool flag=false;
int count=0;
for(int i=index; i<=n;i++)
{
if(s[i]==in_str.s[j])
{
flag=true;
}
else
{
flag=false;
break;
}
j++;
}
if(strlen(in_str.s)== n && flag)
return 0;
else if(strlen(in_str.s)>n && flag)
return -1;
else
return 1;
}
//Returns the position of the first occurence of ch.
// Return -1 on failure
int find(char ch)
{
for(int i=0;i<strlen(s); i++)
{
if(s[i]==ch)
{
return i;
}
}
return -1;
}
// Returns the position of the first occurence of substring starting at index.
// Returns -1 on failure.
// Outputs the string
int find(MyString& in_str, int index)
{
string sub=in_str.s;
string substrin=s;
int pos=substrin.find(sub, index);
return pos;
}
int empty()
{
if(strlen(s)==0)
return 1;
else
return 0;
}
friend ostream& operator <<(ostream& os, MyString& in_str)
{
os<<in_str.s;
return os;
}
// Concantenates two strings
friend MyString operator+(MyString& in_str1, MyString& in_str2)
{
const char* s1=in_str1.s;
const char* s2=in_str2.s;
int j=0, i;
MyString newString;
newString.s=in_str1.s;
strcpy(in_str1.s, in_str2.s);
cout<<in_str1.s;
newString.s=in_str1.s;
return newString;
}
private:
char* s;
};
int main()
{
char* in_string = "Hello World!";
MyString str(in_string);
cout << str << endl; // Prints Hello World!
cout << str.length() << endl; // Prints 12
str.clear();
cout << str.length() << endl; // Prints a blank
if (str.empty())
cout << "Empty" << endl; // Prints empty
else
cout << "Not empty" << endl;
MyString str1("Howdy"), str2("HelloHowdy");
cout << str1 << " " << str2 << endl; // Prints Howdy HelloHowdy
cout <<str2.compare(5,5,str1) << endl; // Prints 0
cout << str1.find('t') << endl; // Prints -1
cout << str2.find(str1,2) << endl; // Prints 5
MyString str3 = str2+str1;
cout << str3 << endl; // Prints HelloHowdyHowdy
MyString str4(str1);
cout << str4 << endl; // Prints Howdy
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.