[Solved] Finding a string element in an array C++


Your problem is that your return – 1 should be after for loop. When you check whether your string is somewhere in your array you do it only one time, because of the fact that after an if statement the program sees return – 1 which means “oh I should exit the function now and return -1”.

for (int i = 0; i < len; ++i)
 {
     if (arr[i].find(seek) != string::npos)return i; 
 }
 return -1;

When you got such a simple problems I recommend you to use debugger which will tell you exactly what you are doing in your program.

1

solved Finding a string element in an array C++