Thread: How to find strings that match specific pattern

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    How to find strings that match specific pattern

    Hi guys.
    I'm working on strings in C and I'm struggling with it about one day so I guess and believe we can solve it altogether cooperatively;
    what I'm trying to do is, assuming I have a specific pattern of string like "CBOARD$ ((string that's written here)) PROGRAMMING$ (string that's written here))", and what I want to do is to print the string where the ((string that's written here)) present, meaning by an example if I have ""CBOARD$ BLA BLA BLA PROGRAMMING$ bla bla bla" I want to print the string which is between CBOARD$ AND PROGRAMMING$, also to print the string that's presented after PROGRAMMING$, meaning to print on the screen:
    BLA BLA BLA
    bla bla bla

    the pattern CBOARD$ <string1> PROGRAMMING$ <string2> is constant, and always need to print the <string1> and the <string2>.


    frankly I have made a code but it's crashed while compiling, any help please?! I appreciate your cooperation.

    thanks in advance.

  2. #2
    Guest
    Guest
    Always post your code so people can chime in. I imagine you'd want to use standard library functions that search string1 inside string2 to locate the indices of CBOARD$ (subtract its length) and PROGRAMMING$ and then copy the section of string in between. You could of course roll your own function that only seeks through the source string once, but I would chose the first (more robust) option.

    Another solution might be to use a regular expression library that can return the matching string for you without much code.

  3. #3
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by Guest View Post
    Always post your code so people can chime in. I imagine you'd want to use standard library functions that search string1 inside string2 to locate the indices of CBOARD$ (subtract its length) and PROGRAMMING$ and then copy the section of string in between. You could of course roll your own function that only seeks through the source string once, but I would chose the first (more robust) option.

    Another solution might be to use a regular expression library that can return the matching string for you without much code.
    I understand you, but the question is how can I separate the strings from the pattern?! by strcut? but it doesn't work.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by RyanC
    I understand you, but the question is how can I separate the strings from the pattern?! by strcut? but it doesn't work.
    strcut is not a standard library function. I'm not sure what you mean by "separate the strings from the pattern". Don't you already know what are the delimiter strings (e.g., you know beforehand that you want to use CBOARD$ to denote the start delimiter for the first string, and PROGRAMMING$ as the end delimiter of the first string)? Or are you saying that you're trying to identify them, and then extract the strings?

    If you already know what they are, then as Guest describes, it is actually pretty simple: two calls to strstr will do (and maybe two corresponding strlen calls if you don't also know how long the delimiters are beforehand), and the rest is just the string extraction which can be done with two loops or with careful calls to say, strncpy.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by laserlight View Post
    Don't you already know what are the delimiter strings (e.g., you know beforehand that you want to use CBOARD$ to denote the start delimiter for the first string, and PROGRAMMING$ as the end delimiter of the first string)? Or are you saying that you're trying to identify them, and then extract the strings?
    .
    Code:
    Exactly, I know beforehand what are the delimiters ! , I already done as what Adriano tell but it still give me an Error compilation, meaning it doesn't even show something on the screen.
    Secondly, I just searched on this forum about Strings's posts and want to verify something really confusing, I found we must declare like this statement(State is a pointer to String) : State[strlen[State]-1]='\0' in order to put '\0' in last char's places for getting a suitable string: but what why '\0' at index "strlen[State] -1 " , it should be at State[strlen]='\0' ; for instance lets assume I have a char array with size 50, char arr[50]={'h','e'}; so the strlen of arr here is 2, and I must but '\0' at third place which is strlen(arr) and not strlen(arr)-1 in order to get suitable string

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want help with resolving your compile error, then you should post the code and the compile error.

    As for your question: no, that won't work because strlen depends on the presence of a null character, so you cannot use it to help you terminate a string. Perhaps you are thinking of sizeof, which can help if you fully utilise an array to store a string, and that would explain why you would subtract 1: that computes the last valid index of the array. Anyway, it is not a "must" as there are other ways to determine the place to terminate a string with a null character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. State design pattern - can't find an error
    By ZeroesAndOnes in forum C++ Programming
    Replies: 12
    Last Post: 03-28-2017, 03:26 AM
  2. Replies: 16
    Last Post: 02-23-2016, 02:21 AM
  3. Program to match strings from input to files using dfa
    By Praveena Rao K in forum C Programming
    Replies: 3
    Last Post: 09-04-2013, 07:11 AM
  4. pattern matching in strings
    By roaan in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 06:30 AM
  5. Help me to find the occurence of a pattern
    By Tintu in forum C Programming
    Replies: 4
    Last Post: 10-05-2007, 12:18 AM

Tags for this Thread