0
(Array Problem C++) So if someone can help me break this down for me and help me find the error.
So what I'm trying to do is to 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 the 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. bool locatesequence (const string a[], int n, string target, int& begin, int& end){ bool flag = false; if (n < 0) return false; for(int i = 0; i < n; i++){ if(a[i] == target && flag == false) end = i; } return flag; }
1 Answer
0
When you check a[i] == you compare a character and a string. You should use something like a.substr(i, target.length()) == target, or a.compare(i, target.length(), target) == 0 (last one is a bit better since it does not create a new string)
All that is if you want to write string search function yourself. Otherwise just use a.find(target)