Thread: pattern search

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    16

    pattern search

    hi,
    I just started working with C.
    How do we write a C function that matches two strings like ...
    $string1 = NY PROD Box;
    string2 = NJ PROD Box;

    I would like a function that basically will check like perl.
    Code:
    if ($string1 =~ "[A-Z]+ PROD Box"){
    code 
     }
    Thank you,

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One way to do it in C would be to have a char array with the string you want to check, and another one have the string you want it to be checked against it. Then use the strcmp() function. If it returns zero, the strings are the same.

    You need to include <string.h> and then
    Code:
    if((strcmp(string1, string2)) == 0)
       //it's a matching string
    where string1 and string2 are pointers to the char arrays base address (name of the array can also be used for this).

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    hi
    The problem is that the string1 and string2 are not exact match.
    Only the last of part ie the "PROD Box " part of the string matches.

    so I am not sure if the strcmp works here.

    Any other suggestions?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    size_t len = strlen(string1);
    size_t len2 = strlen("PROD Box");
    if(len < len2) return 0;
    
    if(strcmp(string1+len-len2, "PROD Box") == 0) return 1;
    
    return 0;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The obvious answer.... use regular expressions in C.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    16
    I think there will be a little problem with the above logic.

    What if string1 = "NY TEST Box"

    Then the above login will return true even for the NY TEST Box
    (Correct me if I am wrong)

  7. #7
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Just check if the last 8 chars of the string are "PROD Box". If the string is less than 8 chars it's false by default. Regular expressions are overkill if you just want to match the suffix of two strings.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by vinuk23 View Post
    I think there will be a little problem with the above logic.

    What if string1 = "NY TEST Box"

    Then the above login will return true even for the NY TEST Box
    (Correct me if I am wrong)
    You're wrong. The last eight characters must match "PROD Box".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string pattern search problem
    By goron350 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 08:50 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. pattern search
    By _Cl0wn_ in forum C Programming
    Replies: 3
    Last Post: 02-19-2003, 05:51 PM