IMPLEMENT the Following instructions into the String.hpp and String.cpp provided
ID: 3841315 • 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:
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).
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.
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.
Implement a method std::vector<String> String::split(char) const;
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
int String::find(char ch, int start_pos) const
{
int foundpos = start_pos;
while(*(str+foundpos) != ch)
{
foundpos++;
}
if(*(str+foundpos) != '')
{
return foundpos - start_pos;
}
else
{
return -1;
}
}
std::vector<String> String::split(char) const
{
int startposcurstr = 0;
int countfromstart = find(ch, startposcurstr);
std::vector<String> result;
while(countfromstart != -1)
{
result.push_back(substr(startposcurstr, countfromstart));
startposcurstr = startposcurstr + countfromstart + 1;
countfromstart = find(ch, startposcurstr)
}
result.push_back(substr(startposcurstr, string_size - startposcurstr -1);
return result;
}