pattern search

This is a discussion on pattern search within the C Programming forums, part of the General Programming Boards category; hi, I just started working with C. How do we write a C function that matches two strings like ... ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    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;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    The obvious answer.... use regular expressions in C.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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,185
    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, 07: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, 09:33 AM
  4. pattern search
    By _Cl0wn_ in forum C Programming
    Replies: 3
    Last Post: 02-19-2003, 04:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21