1. Provide the definition of the following class MyString. The test function and
ID: 3833311 • Letter: 1
Question
1. Provide the definition of the following class MyString. The test function and the expected output are provided.
else
}
2. Below is an example of a sorting algorithm that sorts a list of numbers in ascending order. Write a function
void mySort(vector<int>& num_list, int num_elements)
that implements the algorithm for an input vector num list of num element integers. The test function and the expected output are provided.
Here’s the example - Let us take the array of numbers ”5 1 4 2 8”, and sort the array from lowest number to greatest number. In each step, elements written in bold are being compared. Three passes will be required.
int main() {
}
Can someone please write the code for both problems in c++?
Thanks!
Explanation / Answer
PROGRAM CODE:
#include <iostream>
#include <string.h>
using namespace std;
class MyString {
public:
MyString()
{
s = new char[100];
}
MyString(const char* c_string)
{
s = new char[100];
int i;
for( i=0; i<strlen(c_string); i++)
s[i] = c_string[i];
s[i] = '';
}
MyString(const MyString& in_str)
{
s = new char[100];
int i;
for( i=0; i<strlen(in_str.s); i++)
s[i] = in_str.s[i];
s[i] = '';
}
~MyString()
{
}
int length() const // Returns length of string
{
return strlen(s);
}
void clear() // Erases the contents of the string, which becomes an empty string
{
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 unpto n characters
int compare(int index, int n, const MyString& in_str)const
{
for(int i=0; i<n; i++)
{
if(s[index+i] < in_str.s[i])
return 1;
else if(s[index+i] > in_str.s[i])
return -1;
}
return 0;
}
//Returns the position of the first occurence of ch.
// Return -1 on failure
int find(char ch) const
{
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.
int find(const MyString& in_str, int index) const
{
for(int i=0; i<strlen(s); i++)
{
if(s[i] == in_str.s[index-1])
return i+1;
}
return -1;
}
bool empty()
{
return s == "";
}
// Outputs the string
friend ostream& operator <<(ostream& os, const MyString& in_str)
{
os<<in_str.s;
return os;
}
// Concantenates two strings
friend const MyString operator+(MyString& in_str1, MyString& in_str2)
{
int len = 0;
char *newString = new char[100];
for(int i=0; i<strlen(in_str1.s); i++)
newString[len++] = in_str1.s[i];
for(int i=0; i<strlen(in_str2.s); i++)
newString[len++] = in_str2.s[i];
newString[len] = '';
MyString newStr(newString);
return newStr;
}
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;
}
OUTPUT:
PROGRAM CODE:
#include <iostream>
#include <vector>
using namespace std;
void mySort(vector<int>& num_list, int num_elements);
int main() {
vector<int> test_vec;
int num_ele = 5;
for (int i = 0; i < num_ele; i++)
test_vec.push_back(num_ele - i);
mySort (test_vec, num_ele);
for (int i = 0; i < num_ele; i++)
cout << test_vec.at(i) << " " << endl; // Should print 5 4 3 2 1
return 0;
}
void mySort(vector<int>& num_list, int num_elements)
{
for(int i=0; i<num_elements; i++)
{
for(int j=i+1; j<num_elements; j++)
{
if(num_list[i] < num_list[j])
{
int temp = num_list[i];
num_list[i] = num_list[j];
num_list[j] = temp;
}
}
}
}
OUTPUT:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.