Copy selected characters between strings?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nigelmercier
    New Member
    • Dec 2009
    • 45

    Copy selected characters between strings?

    I have two strings which are defined thus:

    char txt4[4], txt7[7];

    The string txt7 holds the character representation of an integer (0 to 9999), via IntToStr, which is defined thus:

    Converts input signed integer number to a string. The output string has fixed width of 7 characters including null character at the end (string termination). The output string is right justified and the remaining positions on the left (if any) are filled with blanks.Requires Destination string should be at least 7 characters in length.

    The integers I'm converting have 4 digits and I want txt4 to hold these associated characters from txt7 (There is no sign character, and I'm not interested in the null terminator, the display routine is custom built).

    What is a simple way to copy these 4 characters from txt7 to txt4?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    strncpy? From tx7+2 for a length of 4?

    Comment

    • nigelmercier
      New Member
      • Dec 2009
      • 45

      #3
      I figured strncpy would be the one, but I'm not 100% sure on now to do the txt7+2 bit. I had tried this and it didn't work:

      strncpy(txt4, txt7[2], 4) ;

      I guess pointers are involved?

      [Later] Ignore that, it seems that txt7+2 is actually what I need, just like you said.

      Many thanks.

      Comment

      Working...