Question 5(15pt): split astring When you're processing data, it's useful to break up a textstring into pieces
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 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 < text.length();i++) { if (text[i] ==seperator) { if(word.length() == 0) continue; list[j] =word; j++; if(j ==length) { //cout << "Content in String array :"; for(int i1 = 0 ; i1 < j ; i1++) { cout << list[i1] << endl; } return length; } word =""; } else { word = word+ text[i]; } } //cout << "Content in String array :"; for(int i1 = 0 ; i1 < j ; i1++) { //cout << list[i1] <
int main(){int maxNum;string str;char delimt;cout<<"Enter String : ";cin>>str;cout<<"Enter Charecter : ";cin>>delimt;cout<<"Enter Maximum Number : ";cin>>maxNum;string words[maxNum];cout<<"Number of Pieces : "<