# Thread: Checking array for string

1. ## Checking array for string

Let's say I've got an array full of strings (A short string on every line). Now I just want to check this array if a certain string exists within it. It doesn't even matter on which "line" of the array it is located. I just need to know whether it's there or not.

How can I do this?

2. The basic method for doing that would be to just iterate through the string and compare each line with the target string until you either find the string or reach the end of the list.

If you want to do it more quickly, make sure the list of words/strings is sorted in alphabetical order and do a binary search based on the return value from strcmp(). That will be log2(n) instead of n in complexity.

--
Mats

3. The array won't be longer than about 100 lines. I can't really put it in alphabetical order because the order of the lines in the array depends another part in the program.

Would this condition work?:

Code:
`if ("string i'm checking for" = array_of_strings[i]){do something;}`

4. In C, the only (standard) way to compare strings is to use strcmp(). And note that strcmp() returns 0 when strings are equal.

--
Mats

5. = is assign, too, not compare.
if (x = 0) would assign 0 to x and we get if (x) which is basically if (x != 0).
On the other hand, comparison is ==, so it should be if (x == 0).
Also note that the in C, strings cannot be compared with the == operator, but with strcmp, as mats points out.

6. Ah, strcmp(). There are so many functions I still don't know.
I've made this function, which should return a true or false for comparison:

Code:
```int stringcheck(char string1_to_check_for,char string2_to_check_for,char* array_to_check[],int length_of_array,){

int result=0;

while (result == 0){
if( strcmp(string1_to_check_for,array_to_check[i])==0 | strcmp(string2_to_check_for,array_to_check[i])==0 ){
result = 1;
}else{
result=0;
i++;
}
}

return result;
}```
Am I getting this right?

And another question:
Is it necessary in every compiler, in order to test a little c file, to first create a project, add the c file, add linkers, and then finally compile it??
It's a little pain in the ass if I just want to test for instance this function, without using it in my program right away.

7. Originally Posted by Ayreon
Ah, strcmp(). There are so many functions I still don't know.
I've made this function, which should return a true or false for comparison:

Code:
```int stringcheck(char string1_to_check_for,char string2_to_check_for,char* array_to_check[],int length_of_array,){

int result=0;

while (result == 0){
if( strcmp(string1_to_check_for,array_to_check[i])==0 | strcmp(string2_to_check_for,array_to_check[i])==0 ){
result = 1;
}else{
result=0;
i++;
}
}

return result;
}```
Am I getting this right?
I doubt it. Despite the name, string1_to_check_for is not a string, nor is string2_to_check_for. Logical OR is ||. Do you intend to check every string in the universe, or just the strings in your array?
And another question:
Is it necessary in every compiler, in order to test a little c file, to first create a project, add the c file, add linkers, and then finally compile it??
It's a little pain in the ass if I just want to test for instance this function, without using it in my program right away.
Only if your IDE isn't very smart. VS is not very smart in this respect, despite everything else. I usually keep a project called "temp" that gets all my scratch code.

8. For the code, no... Firstly, a string in C is a set of two or more characters, so you must take a pointer to the first element of the array for the strings. And since the function don't modify the strings, they should be const char*, even.
A 2D array can't be passed as an array of pointers... You want to make it accept a 2D array (an array of strings) instead. Do this by const char array[][y] (note that the "y" size must be specified or it won't work).
And as for the loop... more common for this type of thing is for loop.
And then there's the if... You are using bitwise OR (|) instead of logical OR (||).
And you probably want to work on your indentation.

As for the question, no. The compilers don't care if you have projects or not. An IDE might care, though.
But you might create a temp or test project where you experiment with stuff instead of creating new projects all the time. That works quite well.

9. You probably should use || here:
Code:
`strcmp(string1_to_check_for,array_to_check[i])==0 | strcmp(string2_to_check_for,array_to_check[i])==0`
as it prevents the second strcmp() from happening (that is probably not an issue here, but there is a distrinct difference in behaviour between || and |.

Code:
```               result=0;
i++;```
Red line is not needed - it was set to zero before.

You may also want to use a for-loop with i starting at zero and ending when you reach "length_of_array". In the posted code, there isn't a declaration of i.

10. I still don't get that const thing.
The string_to_check in my actual program is not a constant, it changes from time to time.

Also, I don't understand why I need to use "const char array[][y]". This one too, is not a constant, throughout the program there can be new additions to this array. Does the y stand for the length of the strings itself?

Before I typed in this function i did use a for loop, but for some reason i thought it could't work, so I used a while instead. Can I use the while loop anyway, for the time being? I can look into it once this works.

11. Originally Posted by Ayreon
I still don't get that const thing.
The string_to_check in my actual program is not a constant, it changes from time to time.

Also, I don't understand why I need to use "const char array[][y]". This one too, is not a constant, throughout the program there can be new additions to this array. Does the y stand for the length of the strings itself?
Const says that the argument cannot be modified.
Since this is a comparison function, the caller can and would expect the contents of the arrays it passes in NOT to change after the function call is finished.
In order to keep this promise, it's possible to add const to the arguments to the function, so it cannot modify what the arguments point to.
Remember - the arguments are local pointers which contains the addresses you pass in. Therefore, you can make those local copies const without affecting the original arrays.

Before I typed in this function i did use a for loop, but for some reason i thought it could't work, so I used a while instead. Can I use the while loop anyway, for the time being? I can look into it once this works.
Sure, that's up to you.

12. Originally Posted by Ayreon
I still don't get that const thing.
The string_to_check in my actual program is not a constant, it changes from time to time.

Also, I don't understand why I need to use "const char array[][y]". This one too, is not a constant, throughout the program there can be new additions to this array. Does the y stand for the length of the strings itself?

Before I typed in this function i did use a for loop, but for some reason i thought it could't work, so I used a while instead. Can I use the while loop anyway, for the time being? I can look into it once this works.
Well, you do need to break the loop when/if you find what you are looking for, but doing so as a "break" when the if-statement is true is a good way, as it reduces the conditions being tested when it's not true.

--
Mats

13. Now I think I see what goes wrong with the while loop. It is not sure that the strings i'm looking for are present in the array. In that case the loop would just go on until i gets bigger than the array_to_check.

But I don't know how to make the for loop work.

Code:
```for(i=0;i<length_of_array;i++){
if( strcmp(string1_to_check_for,array_to_check[i])==0 || strcmp(string2_to_check_for,array_to_check[i])==0 ){
result = 1;
}

}```
Now I only have an if, won't i need an else?

14. Originally Posted by Ayreon
Now I think I see what goes wrong with the while loop. It is not sure that the strings i'm looking for are present in the array. In that case the loop would just go on until i gets bigger than the array_to_check.

But I don't know how to make the for loop work.

Code:
```for(i=0;i<length_of_array;i++){
if( strcmp(string1_to_check_for,array_to_check[i])==0 || strcmp(string2_to_check_for,array_to_check[i])==0 ){
result = 1;
}

}```
Now I only have an if, won't i need an else?
You can put one in, if you want. Since you don't need to do anything in the else case, why bother?

15. Correct. But you do need to somehow check if you need to continue looking (that will, on average, speed up your search by factor of 2, since on average [assuming equal distribution of course] you will only need to search through HALF of the items. Put a break after "result = 1". Or if you are that way inclined, return 1, and return 0 after the loop.

--
Mats