Searching a pointer for CRLF

Hi,

I have a char pointer which points to the beginning of lines of characters. Each line is terminated with carriage return - line feef (CRLF) pair .

Can someone tell me how do I go about searching for CRLF pair in the pointer?

Thanks

Comments

  • You can use strstr() to find the first location of a substring. Since you need subsequent finds as well, you'll need a floating pointer to use such that you start each next search from the point after the previous find.

    Here's an example:

    [code]
    #include
    #include

    using namespace std;

    int main(int argc, char *argv[])
    {
    char *string1 = "ABCDEFGHIJKLMFIRSTSTUVWXYZABCDEFGHIJKLMSECONDTUVWXYZ";
    char *location;

    location = strstr(string1, "LM");
    if (location)
    printf("%s

    ", location+2);

    location = strstr(location+2, "LM");
    if (location)
    printf("%s

    ", location+2);

    system("PAUSE");
    return EXIT_SUCCESS;
    }
    [/code]

    Take Care,
    Ed
  • Thanks for your reply.

    However, I have just one more question: how can I print the string that
    lies in between the two LMs? i.e (FIRSTSTUVWXYZABCDEFGHIJK )

    thanks
  • You have a pointer to the string to strstr(). Simply print the contents (character) of that pointer and increase it by 1 until everything is printed.
  • Or, you can use strncpy to move the portion of the original string into a second one. You would need to add a second string and a second pointer:

    [code]
    #include
    #include

    using namespace std;

    int main(int argc, char *argv[])
    {
    char *string1 = "ABCDEFGHIJKLMFIRSTSTUVWXYZABCDEFGHIJKLMSECONDTUVWXYZ";
    char string2[52] = "Didn't work!";
    char *location1, *location2;

    location1 = strstr(string1, "LM");
    if (location1)
    printf("%s

    ", location1+2);

    location2 = strstr(location1+2, "LM");
    if (location2)
    printf("%s

    ", location2+2);

    if (location1 && location2)
    strncpy(string2, location1+2, location2-(location1+2));

    printf("%s

    ", string2);


    system("PAUSE");
    return EXIT_SUCCESS;
    }
    [/code]

    Take Care,
    Ed
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories