Question: Question 5(15pt): split astring When you're processing data, it's useful to break up a textstring into pieces using a delimiter. Write a function split thattakes

When you're processing data, it's useful to break up a text string

 

Question 5(15pt): split astring When you're processing data, it's useful to break up a textstring into pieces using a delimiter. Write a function split thattakes a string, splits it at every occurrence of a delimiter, andthen populates an array of strings with the split pieces, up to theprovided maximum number of pieces. Function specifications: ? Thefunction name: split ? The function parameters (in this order): ?The string to be split ? A separator, char, which marks where thestring should be split up ? An array of string, where thesplit-apart string pieces will be stored ? The size of the array,int ? The function should not print anything ? The function returnsthe number of pieces the string was split into, as an integer.Note: ? No input will have delimiters in the beginning or the endof the string. (Eg: ,apple, orange OR apple, orange,) ? No inputwill have multiple delimiters added consecutively. (Eg:apple,,,orange,banana) ? If the delimiter character is not found,then the function returns 1 and the entire string is placed in thearray as the first element. ? If the string is split into morepieces than the size of the array (the last parameter), then thefunction returns -1.

 

When the input maxNum (in the main function) orlength (in the split function ) is less than the actual number ofarrays of the string it should return -1. For example: input--> string: 1,1,1,1,1,1,1,1 / input--> seperator:, / input--> maxnum: 5. The actual maxnumshould be 8 but the user entered an input of 5 so it should return-1.

How can I make the program return -1 when needed? Here is mycode:

int split (string text, char seperator,string list[],intlength){ if (text.length() == 0 || length == 0) { return 0; } string word = ""; int j = 0; text = text + seperator; for (int i = 0; i

int main(){int maxNum;string str;char delimt;cout>str;cout>delimt;cout>maxNum;string words[maxNum];cout

When you're processing data, it's useful to break up a text string into pieces using a delimiter. Write a function split that takes and split a string on a delimiter, then it populates the array of strings with the split pieces up to the provided maximum number of pieces. Function specifications: The function name: split The function parameters (in this order): o The string to be split. o A separator, character, which marks where the string should be split up O An array of string, where the split-apart string pieces will be stored o The size of the array, int The function returns the number of pieces the string was split into as an integer. Your function does not print anything. Important considerations: If the string is split into more pieces than the size of the array, then the function returns -1. If the delimiter character is not found, then the function returns 1 and the entire string is placed in the array as the first element. You can assume: 1. There will not be consecutive delimiters 2. The delimiter will not be the first or the last character in the string For example: string words [10]; split("cow/chicken/fish", '/', words, 10); would return 3 and fill the array with "cow", "chicken", "fish". Answer: (penalty regime: 0 %)

Step by Step Solution

3.43 Rating (159 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The issue with the provided code is that it does not handle the case where the number of pieces afte... View full answer

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 Electrical Engineering Questions!