Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Substing in VC++ [modified]

Substing in VC++ [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Programm3r
    wrote on last edited by
    #1

    Hi all, I have this string, that would look something like this: ZA123654PCNAME I want to break the string apart like this: ZA123654 (Please note that this piece is not constant) PCNAME I have tried to get the secondnd part (by doing the following: below), but how can I get the first part, this is if I'm doing it correctly.. if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0) Thanx in advance -- modified at 8:57 Thursday 9th November, 2006

    The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

    C D 2 Replies Last reply
    0
    • P Programm3r

      Hi all, I have this string, that would look something like this: ZA123654PCNAME I want to break the string apart like this: ZA123654 (Please note that this piece is not constant) PCNAME I have tried to get the secondnd part (by doing the following: below), but how can I get the first part, this is if I'm doing it correctly.. if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0) Thanx in advance -- modified at 8:57 Thursday 9th November, 2006

      The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      On which 'parameters' do you split the string ? I mean, how do you know which is the first part of the string and which is the last part of the string ? Are all the strings the same size ? If not, is there a delimiter character ? Or what ? By looking at your code, it seems that the last part is always PCNAME and is always 6 char long. So, you can then compute the lenght of the first part: int Len = strlen(Request) - 6; Then, you can copy up to Len characters of your string in a buffer: strncpy(szBuff,Request,Len); If that's not the case, please provide more information about how you want to split the string.


      Cédric Moonen Software developer
      Charting control [Updated - v1.1]

      P 1 Reply Last reply
      0
      • C Cedric Moonen

        On which 'parameters' do you split the string ? I mean, how do you know which is the first part of the string and which is the last part of the string ? Are all the strings the same size ? If not, is there a delimiter character ? Or what ? By looking at your code, it seems that the last part is always PCNAME and is always 6 char long. So, you can then compute the lenght of the first part: int Len = strlen(Request) - 6; Then, you can copy up to Len characters of your string in a buffer: strncpy(szBuff,Request,Len); If that's not the case, please provide more information about how you want to split the string.


        Cédric Moonen Software developer
        Charting control [Updated - v1.1]

        P Offline
        P Offline
        Programm3r
        wrote on last edited by
        #3

        My bad Cédric ... I forgot to mention that there is a space between the first part and the PCNAME. And yes PCNAME will always be a constant. But thank you so much, I really appreciate the help (code provided works). Regards

        The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

        X 1 Reply Last reply
        0
        • P Programm3r

          Hi all, I have this string, that would look something like this: ZA123654PCNAME I want to break the string apart like this: ZA123654 (Please note that this piece is not constant) PCNAME I have tried to get the secondnd part (by doing the following: below), but how can I get the first part, this is if I'm doing it correctly.. if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0) Thanx in advance -- modified at 8:57 Thursday 9th November, 2006

          The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          « Programm3r » wrote:

          if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0)

          In this context, both strstr() and strncmp() are doing redundant work. If strstr() returns a non-NULL pointer, it means that "PCNAME" was found in Request. Therefore, there's no need to follow that with a call to strncmp(). Similarly, if strstr() returns a NULL pointer, strncpy() will fail. Make sense?


          "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

          "Judge not by the eye but by the heart." - Native American Proverb

          P 1 Reply Last reply
          0
          • P Programm3r

            My bad Cédric ... I forgot to mention that there is a space between the first part and the PCNAME. And yes PCNAME will always be a constant. But thank you so much, I really appreciate the help (code provided works). Regards

            The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

            X Offline
            X Offline
            XtremDev
            wrote on last edited by
            #5

            Just for fun and if your interrested in that solution, try this : char string[] = "abc def ghi"; char * token = strtok(string, " "); while( token ) {     printf("[%s]\n", token);     token = strtok(NULL, " "); }

            P 1 Reply Last reply
            0
            • X XtremDev

              Just for fun and if your interrested in that solution, try this : char string[] = "abc def ghi"; char * token = strtok(string, " "); while( token ) {     printf("[%s]\n", token);     token = strtok(NULL, " "); }

              P Offline
              P Offline
              Programm3r
              wrote on last edited by
              #6

              Thanx XtremDev ... I'll check it out ... :)

              The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

              1 Reply Last reply
              0
              • D David Crow

                « Programm3r » wrote:

                if(strncmp((char *)strstr(Request,(char*)"PCNAME"),(char *)"PCNAME",6)==0)

                In this context, both strstr() and strncmp() are doing redundant work. If strstr() returns a non-NULL pointer, it means that "PCNAME" was found in Request. Therefore, there's no need to follow that with a call to strncmp(). Similarly, if strstr() returns a NULL pointer, strncpy() will fail. Make sense?


                "Approved Workmen Are Not Ashamed" - 2 Timothy 2:15

                "Judge not by the eye but by the heart." - Native American Proverb

                P Offline
                P Offline
                Programm3r
                wrote on last edited by
                #7

                David ... your to clever bro... :) .. but to answer your question ... It makes sense ... kinda :)

                The only programmers that are better than C programmers are those who code in 1's and 0's..... :) :) Programm3r

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups