Thread: C String, How to find a string between two string

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    3

    C String, How to find a string between two string

    Hello,

    I'm not really confident with C programming and it <string.h> library.

    what i am trying to do is to extract a string between two string.

    ex:
    If I have "xxxxxxxSTARTmessageENDxxxxxxxxx" I need a function that will return "message" which is between "START" and "END"

    Can anyone help me to write such a function or know any easier library easier than (string.h) please? Is (Better String library) good?

    Thank you.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Make use of the strstr function.
    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

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    Quote Originally Posted by laserlight View Post
    Make use of the strstr function.
    hello,
    OK, so i will do something like that:

    Code:
    A= "xxxxxxxSTARTmessageENDxxxxxxxxx"
    B = strstr (A,"START"),
    C = strstr (A, "END"),
    I'll have B:"STARTmessageENDxxxxxxxxx"
    C:"ENDxxxxxxxxx"

    which function will allow me to do:
    B = B - C then B = B - "START"

    Thank you.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that if you compute B + strlen("START") you will get a pointer to the first character of "messageENDxxxxxxxxx". Since you also have C, you can then copy over the characters in between (or simply set *C to '\0').
    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
    Registered User
    Join Date
    Aug 2012
    Posts
    3
    Sorry, I know it must be obvoius for you but i'm not sure what do you mean by "copy over the characters in between"? which function you are talking about?
    Thank you.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is probably good practice for you to write a loop to do that.
    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. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  2. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  3. Find index of last char in search string in string?
    By Programmer_P in forum C++ Programming
    Replies: 6
    Last Post: 06-07-2010, 06:51 PM
  4. find a string based on the location of another string
    By rlilley in forum C Programming
    Replies: 3
    Last Post: 02-19-2009, 12:29 PM
  5. Replies: 1
    Last Post: 10-31-2005, 11:36 AM

Tags for this Thread