C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Searching a pointer for CRLF Posted by renegade007 on 31 Oct 2008 at 10:07 PM
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

Report
Re: Searching a pointer for CRLF Posted by Ed Hall on 1 Nov 2008 at 7:54 AM
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:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
 char *string1 = "ABCDEFGHIJKLMFIRSTSTUVWXYZABCDEFGHIJKLMSECONDTUVWXYZ";
 char *location;
 
 location = strstr(string1, "LM");
 if (location)
    printf("%s\n\n", location+2);
 
 location = strstr(location+2, "LM");
 if (location)
    printf("%s\n\n", location+2);

    system("PAUSE");
    return EXIT_SUCCESS;
}


Take Care,
Ed
Report
Re: Searching a pointer for CRLF Posted by renegade007 on 4 Nov 2008 at 12:03 AM
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
Report
Re: Searching a pointer for CRLF Posted by Lundin on 4 Nov 2008 at 12:18 AM
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.
Report
Re: Searching a pointer for CRLF Posted by Ed Hall on 4 Nov 2008 at 9:08 AM
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:

#include <cstdlib>
#include <iostream>

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\n\n", location1+2);
 
 location2 = strstr(location1+2, "LM");
 if (location2)
    printf("%s\n\n", location2+2);

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

 printf("%s\n\n", string2);


    system("PAUSE");
    return EXIT_SUCCESS;
}


Take Care,
Ed



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.