IMPLEMENT the Following instructions into the String.hpp and String.cpp provided
ID: 3842415 • 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: All you have to do is simply implement the BIG BOLD sentences below into the .cpp file.
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::split(char) const;
5) You will use std::vector<> (need to include ) 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
Here are the functions for you.
// to find the substring
String String::substr(int start_pos, int count) const {
char s[count + 1];
int i, pos = 0;
for (int i = start_pos;
(i < string_size - 1) && (pos < count); i++) {
s[pos++] = str[i];
}
s[pos] = '';
return String(s);
}
// to find the position of a character in string
int String::find(char ch, int start_pos) const {
int i = start_pos;
int index = -1;
for (int i = start_pos;
(i < string_size - 1); i++) {
if (str[i] == ch)
return i;
}
return index;
}
// to find a string inside the original string
int String::find(const String & s, int start_pos) const {
int i = start_pos;
int len = s.string_size - 1;
int index = -1;
for (int i = start_pos;
(i < string_size - 1); i++) {
bool found = true;
for (int j = 0;
((j + i) < string_size - 1) && j < len; j++) {
if (str[i + j] != s.str[j]) {
found = false;
break;
}
}
if (found) {
return i;
}
}
return index;
}
// to split the string using a character
std::vector < String > String::split(char ch) const {
std::vector < String > v;
int start = 0;
for (int i = 0; i < string_size - 1; i++) {
// if character matches, provide the string from start to this
// position and then move start to i+1
if (str[i] == ch) {
if (i > start) {
v.push_back(substr(start, i - start));
start = i + 1;
}
}
}
// push the remaining string
if (start < string_size - 1) {
v.push_back(substr(start, string_size - 1));
}
return v;
}