Question: C++ programming! Code template: #include #include #include // TODO: Implement split function here // Do not change main function int main() { std::string line =

C++ programming! Code template:

#include #include #include

// TODO: Implement split function here

// Do not change main function

int main() { std::string line = ""; std::cout << "Enter a string: "; getline(std::cin, line); std::cout << "Enter the separator character: "; char separator = getchar();

std::vector< std::string > parts = split(line, separator); std::cout << "Splitted string including empty parts: " << std::endl; for( auto part : parts ) { std::cout << part << std::endl; }

std::vector< std::string > parts_no_empty = split(line, separator, true); std::cout << "Splitted string ignoring empty parts: " << std::endl; for( auto part : parts_no_empty ) { std::cout << part << std::endl; } }

Instructions:

Implement the function split that splits the string given as a parameter into parts at the separators, and returns the parts stored in a vector. The parameters of the function are:

a string that will be split

a separator

a boolean value revealing whether you want to ignore the empty parts (empty strings), the default is false. (If the aim is to ignore the empty parts, the value true is passed for the function.)

An empty part is created if there are two consecutive separators, or if a string starts or ends with a separator. For example, if the separator is : and the string is A:B::C: the parsed parts are A, B, , C, and . If there are several consecutive separators, a new, empty part is created at each space with no characters. If you want to ignore the empty parts, then the function will not return the parts with no characters.

Add only the implementation of the function into the code template. Do not edit the main function in any way. When you have implemented the function, the main program is supposed to work like this:

Enter a string: a::bc:def::hijlkm Enter the separator character: : Splitted string including empty parts: a bc def hijlkm Splitted string ignoring empty parts: a bc def hijlkm 

Here you have an example that is a bit tougher to read. It includes spaces at the beginning and end of the split string, and as a separator:

Enter a string: A B C D E Enter the separator character: Splitted string including empty parts: A B C D E Splitted string ignoring empty parts: A B C D E 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!