Write a function in C that does the following The function m

Write a function in C++ that does the following:

The function must not use any function templates from the algorithms portion of the Standard C++ library.

bool locateSequence(const string a[], int n, string target, int& begin, int& end);

Find the earliest occurrence in a of one or more consecutive strings that are equal to target; set begin to the position of the first occurrence of target, set end to the last occurrence of target in that earliest consecutive sequence, and return true. If n is negative or if no string in a is equal to target, leave begin and end unchanged and return false. Here\'s an example:

Return the position of the first string in the array that is equal to target. Return 1 if there is no such string. As noted above, case matters: Do not consider \"jOn\" to be equal to \"Jon\". Return the number of strings in the array that are equal to target. [Of course, in this and other functions, if n is negative, the paragraph above that starts \"Notwithstanding\" trumps this by requiring that the function return 1. Also, in the description of this function and the others, when we say \"the array\", we mean the n elements that the function is aware of.] As noted above, case matters: Do not consider \"jon\" to be equal to \"JoN\". The function must write take at least two parameters: an array of strings, and the number of items the function will consider in the array, starting from the beginning.

Notwithstanding each function\'s behavior described below, all functions that return an int must return 1 if they are passed any bad arguments (e.g. a negative array size, or a position that would require looking at the contents of an element past the last element we\'re interested in). Unless otherwise noted, passing 0 to the function as the array size is not itself an error; it merely indicates the function should examine no elements of the array.

Solution

Hi, Please find my implementation

Please let me know in case of any issue:

bool locateSequence(const string a[], int n, string target, int& begin, int& end){
  
// if size of array is passed negatively
if(n < 0)
return false;

int i= 0; // counter
// finding first occerence of \'target\' in a

while(i < n){
// if current element from \'a\' is equal to \'target\', then stop while loop
if(a[i].compare(target) == 0)
break;
}

// \'target\' is not in \'a\'
if(i == n){
return false;
}

// set begin to i
begin = i;

// counting all consuitive occurence of target from i+1
i = i+1;

while((i < n) && (a[i].compare(target) == 0)){
i++;
}

// setting \'end\' to (i-1)
end = i-1;

return true;
}

Write a function in C++ that does the following: The function must not use any function templates from the algorithms portion of the Standard C++ library. bool
Write a function in C++ that does the following: The function must not use any function templates from the algorithms portion of the Standard C++ library. bool

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site