Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a program in C++ that includes a function named extractString that accepts a string named 'from', an integer named 'start', a second integer named
Write a program in C++ that includes a function named extractString that accepts a string named 'from', an integer named 'start', a second integer named 'end', and an empty string 'to'. This function REMOVES all of the characters from the string 'from' starting at index 'start' and stopping at index 'end' and COPIES them into the string 'to', but ONLY if the following conditions are true: a) 'start' MUST be less than 'end' b) 'start' MUST be less than the length of the 'from' string c) 'end' MUST be less than the length of the 'from' string d) 'start' MUST be greater than or equal to 0. If any of the conditions above are NOT met, then this function does nothing! The function returns the number of characters extracted or the value -1 if no extraction occurs. NOTE: Removing a character will require shifting all remaining characters in the'from' string 1 position to the left (will require a nested loop)! For example, removing the 'X' in the string "Xarea", will require moving the 'a', 'r', 'e', 'a', and the null byte '\0' all over to the left 1 position! For example, the following code: int main( ) { int result; char source1[81] = "She sells sea shells by the sea shore", destination1[21] = ""; char source2[81] = "Where do you want to go today?", destination2[21] = ""; result = extractString(source1, 10, 13, destination1); // removes the string 'sea' and returns 3 cout << "source1: " << source1 << ", dest1: " << destination1 << ", result: " << result << endl; result = extractString(source2, 50, 63, destination2); // does not alter ANY string and returns -1 cout << "source2: " << source2 << ", dest2: " << destination2 << ", result: " << result << endl; return 0; } Will display: source1: She sells shells by the sea shore, dest1: sea, result: 3 source2: Where do you want to go today?, dest2: , result: -1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started