Checking if a string exists in an Array

 

If for example I made a string, a bool, and a string array,

string A = "CCC333";
bool Check;
string List[4] = {"AAA111", "BBB222", "CCC333", "DDD444"};

And I want to check if the string A exists in the list (where the bool "Check" should turn out true), how should the code be written?

I rarely use arrays so I have no idea how this works.

Thanks for the help in advance!

 
I am on mobile, so no code example. But let me at least provide an outline.

There are multiple approaches.

First would be a for() or a while() loop.
You would go through the elements one by one and do a comparison.

The comparison could be done by case sensitive or case insensitive operation. Also it could be done using String Find function.

This opens up multiple types of search operations.

You could have a search term that does not match perfectly in this sense, but still would provide you with a result.

So a fuzzy search could be performed.


Another approach could be to transform / cast the search term and the array into a char type.

This approach would remove the original borders of the string elements and enable you to search for a term which "concatenates" the result across the original formation of the string array.

This approach would need two loops (for or while) nested into each other. The outer loop searches for the first character match and the nested loop compares the following characters for a match. (Here also a case insensitive match could be performed) if the inner loop "disapproves", the outer loop continues with the search of the first char at where the inner loop has stopped.

Well, all this is only an outline, it is comparable to speed and results, as the approach will already determine what type of input you are expecting.

In your example though, I would probably go with the first.  Initially I would lower case the search term and get its length. Then I would at first comparison Check length equality, then transform the arrays string to lower case and do a direct comparison of the search term and the array term.

As they match, the bool gets true. This bool variable would be a condition to my while loop. So as this gets true, the loop stops.

I would use an index variable to loop the array elements, which only gets incremented if the bool variable is false.

There I would have my result. An index variable showing me the array element, as well as a bool variable to know if the search was successful.


 
Dominik Egert #:
I am on mobile, so no code example. But let me at least provide an outline.

There are multiple approaches.

First would be a for() or a while() loop.
You would go through the elements one by one and do a comparison.

The comparison could be done by case sensitive or case insensitive operation. Also it could be done using String Find function.

This opens up multiple types of search operations.

You could have a search term that does not match perfectly in this sense, but still would provide you with a result.

So a fuzzy search could be performed.


Another approach could be to transform / cast the search term and the array into a char type.

This approach would remove the original borders of the string elements and enable you to search for a term which "concatenates" the result across the original formation of the string array.

This approach would need two loops (for or while) nested into each other. The outer loop searches for the first character match and the nested loop compares the following characters for a match. (Here also a case insensitive match could be performed) if the inner loop "disapproves", the outer loop continues with the search of the first char at where the inner loop has stopped.

Well, all this is only an outline, it is comparable to speed and results, as the approach will already determine what type of input you are expecting.

In your example though, I would probably go with the first.  Initially I would lower case the search term and get its length. Then I would at first comparison Check length equality, then transform the arrays string to lower case and do a direct comparison of the search term and the array term.

As they match, the bool gets true. This bool variable would be a condition to my while loop. So as this gets true, the loop stops.

I would use an index variable to loop the array elements, which only gets incremented if the bool variable is false.

There I would have my result. An index variable showing me the array element, as well as a bool variable to know if the search was successful.


Thanks for the idea! Here's my attempt:

string A = "CCC333";
bool Check;
string List[4] = {"AAA111", "BBB222", "CCC333", "DDD444"};

for (int i=0; i<ArraySize(List); i++)
        {
        if (StringFind(List[i],A) != -1)
                {
                Check = true;
                }
        else
                {
                Check = false;
                }
        }
 
Austin John Villarico Salvador:

If for example I made a string, a bool, and a string array,

And I want to check if the string A exists in the list (where the bool "Check" should turn out true), how should the code be written?

I rarely use arrays so I have no idea how this works.

Thanks for the help in advance!

   string A = "CCC333";
   bool Check;
   string List[4] = {"AAA111", "BBB222", "CCC333", "DDD444"};
   for(int i = 0; i < ArraySize(List); i++)
     {
      if(A == List[i])
        {
         Check = true;
         break;
        }
     }
 
MOHAMED SABER MOHAMED AHMED MOUSTAFA #:

That's much simpler than what I have attempted. Thanks a lot!

 
Write a function once and use anywhere.
template<typename T> int array_contains(const T& arr[], T what){
   int i=ArraySize(arr); while(--i >= 0) if(arr[i] == what) break;
   return i;
}

   string A = "CCC333";
   string List[] = {"AAA111", "BBB222", "CCC333", "DDD444"};
   bool Check = array_contains[List, A] >= 0;
 
William Roeder #:
Write a function once and use anywhere.
Great solution! 
Reason: