Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IMPLEMENT the Following instructions into the String.hpp and String.cpp provided

ID: 3842404 • Letter: I

Question

IMPLEMENT the Following instructions into the String.hpp and String.cpp provided. DO NOT just simply copy the code I give you as your answer

Intructions:

Implementation:

For your String class:

1) Implement method String String::substr(int start_pos, int count) const;. This will return the specified substring starting at start_pos and consisting of count characters (there will be less than count if it extends past the end of the string).

2) Implement method int String::find(char ch, int start_pos) const; which gives the first location starting at start_pos of the character ch or -1 if not found.

3) Implement method int String::find(const String & s, int start_pos) const; which gives the first location starting at start_pos of the substring s or -1 if not found.

4) Implement a method std::vector<String> String::split(char) const;

5) You will use std::vector<> (need to include <vector>) for storing the results of parsing the input data.

This method will return a vector of String split up based on a supplied character. For example given s = "abc ef gh", the call s.split(' ') will return a vector with three strings: "abc", "ef", and "gh".

std::vector has a number of operations defined including operator[], and size (number of elements in the vector).

-------------------------------------------------------------------------------------

string.hpp

-------------------------------------------------------------------------------------

-------------------------------------------------------------------------------

string.cpp

-------------------------------------------------------------------------------

Explanation / Answer

Hi,

I have given answer for your first 3 subquestions.

Please include the following snippets to your string main function.

1.Find substring in a given string :

String String::substr(int start_pos, int count) const
{
sub = new char[count+1];
int c = start_pos + count
for(int i = start_pos,j=0;i<c; i ++, j++)
{
if(str[i]!='')
sub[j] = str[i];
else
{sub[j]='';
return sub;
}
}

2. Finding a character from start_pos in given string:

int String::find(char ch, int start_pos) const
{
int count = str.length();
for(int i = start_pos-1; i <= length; i++)
{
if(str[i] == ch)
return start_pos;
else return -1
}
}
}

3. Finding starting position of substring:

int String::find(const String & s, int start_pos) const
{
int count = str.length();
int scount = s.length();
for(int i = start_pos-1, j=0; i <= length, j<scount; i++)
{
if(str[i] == s[j])
j++;
else
j=0;
}
if(j>1)
return i - j +1;
else
return -1;
}