Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Fundamentals Concepts, Drivers & Techniques

Authors: Thomas Erl, Wajid Khattak, Paul Buhler

1st Edition

0134291204, 9780134291208

More Books

Students also viewed these Databases questions