[C] copy string question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alan

    [C] copy string question

    hi, i want to separate a string into different parts and copy them to other
    array, how can i do that?

    Char s[11] = "12345.6789 ";
    Char long_d[4] = "000",
    long_m[8] = "00.0000";

    e.g. i want to copy s to long_d and long_m so that
    long_d[4] = "123" and long_m[8] = "45.6789"

    thx


  • Artie Gold

    #2
    Re: [C] copy string question

    Alan wrote:[color=blue]
    > hi, i want to separate a string into different parts and copy them to other
    > array, how can i do that?
    >
    > Char s[11] = "12345.6789 ";[/color]
    Erm, that's `char', not `Char'.[color=blue]
    > Char long_d[4] = "000",[/color]
    Similarly.[color=blue]
    > long_m[8] = "00.0000";
    >
    > e.g. i want to copy s to long_d and long_m so that
    > long_d[4] = "123" and long_m[8] = "45.6789"
    >[/color]

    Use strncpy().

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas

    "Yeah. It's an urban legend. But it's a *great* urban legend!"

    Comment

    • Alan

      #3
      Re: [C] copy string question



      "Artie Gold" wrote:[color=blue]
      > Alan wrote:[color=green]
      > > hi, i want to separate a string into different parts and copy them to[/color][/color]
      other[color=blue][color=green]
      > > array, how can i do that?
      > >
      > > Char s[11] = "12345.6789 ";[/color]
      > Erm, that's `char', not `Char'.[color=green]
      > > Char long_d[4] = "000",[/color]
      > Similarly.[color=green]
      > > long_m[8] = "00.0000";
      > >
      > > e.g. i want to copy s to long_d and long_m so that
      > > long_d[4] = "123" and long_m[8] = "45.6789"
      > >[/color]
      >
      > Use strncpy().[/color]

      but when i use
      strncpy(long_d, s,3);
      strncpy(long_m, s,7);
      i just got long_d[4] = "123" and long_m[8] = "12345.6"

      [color=blue]
      >
      > HTH,
      > --ag
      >
      > --
      > Artie Gold -- Austin, Texas
      >
      > "Yeah. It's an urban legend. But it's a *great* urban legend!"[/color]


      Comment

      • Jeff Schwab

        #4
        Re: [C] copy string question

        Alan wrote:[color=blue]
        > "Artie Gold" wrote:
        >[color=green]
        >>Alan wrote:
        >>[color=darkred]
        >>>hi, i want to separate a string into different parts and copy them to[/color][/color]
        >
        > other
        >[color=green][color=darkred]
        >>>array, how can i do that?
        >>>
        >>>Char s[11] = "12345.6789 ";[/color]
        >>
        >>Erm, that's `char', not `Char'.
        >>[color=darkred]
        >>>Char long_d[4] = "000",[/color]
        >>
        >>Similarly.
        >>[color=darkred]
        >>> long_m[8] = "00.0000";
        >>>
        >>>e.g. i want to copy s to long_d and long_m so that
        >>>long_d[4] = "123" and long_m[8] = "45.6789"
        >>>[/color]
        >>
        >>Use strncpy().[/color]
        >
        >
        > but when i use
        > strncpy(long_d, s,3);
        > strncpy(long_m, s,7);
        > i just got long_d[4] = "123" and long_m[8] = "12345.6"[/color]

        You can always ignore any number of characters at the beginning of the
        string (up to the length of the string), and still have a valid string.
        In this case, you want to skip the first three:

        strncpy( long_m, s + 3, 7 );

        Since the string you want is indicated by the address of the fourth
        element (which has index 3), you could also do it this way:

        strncpy( long_m, &s[3], 7 );

        Comment

        Working...