Thread: Checking array for string

  1. #1
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79

    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?
    Nothing to see here, move along...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    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;}
    Nothing to see here, move along...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In C, the only (standard) way to compare strings is to use strcmp(). And note that strcmp() returns 0 when strings are equal.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    = 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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    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.
    Nothing to see here, move along...

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Ayreon View Post
    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. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    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.
    Nothing to see here, move along...

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by Ayreon View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Ayreon View Post
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Astrophysics student Ayreon's Avatar
    Join Date
    Mar 2009
    Location
    Netherlands
    Posts
    79
    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?
    Nothing to see here, move along...

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Ayreon View Post
    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. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  2. 2d array question
    By gmanUK in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 12:20 PM
  3. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  4. Checking maximum values for dynamic array...
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 03-21-2005, 12:39 AM
  5. Replies: 5
    Last Post: 05-30-2003, 12:46 AM