C String, How to find a string between two string

This is a discussion on C String, How to find a string between two string within the C Programming forums, part of the General Programming Boards category; Hello, I'm not really confident with C programming and it <string.h> library. what i am trying to do is to ...

  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
    21,794
    Make use of the strstr function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,794
    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').
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,794
    It is probably good practice for you to write a loop to do that.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11:29 AM
  5. Replies: 1
    Last Post: 10-31-2005, 10:36 AM

Tags for this Thread


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