CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2010
    Posts
    25

    copying a string

    i have a question about copying a string.

    basically i have a function that passes an array of characters, it will be a phone number and i want to copy the numbers into a string in the format of the first 3, then the next three then the last 4.

    is there a way to set where i start my copy from in strcpy or is there another function i can use that will assist in doing this?

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: copying a string

    I don't understand... What do you mean, "copy the numbers into a string in the format of the first 3, then the next three then the last 4."? You want three separate strings?

    Viggy

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: copying a string

    Quote Originally Posted by turlisk View Post
    i have a question about copying a string.

    basically i have a function that passes an array of characters, it will be a phone number and i want to copy the numbers into a string in the format of the first 3, then the next three then the last 4.

    is there a way to set where i start my copy from in strcpy or is there another function i can use that will assist in doing this?
    Is your string a char array, or a string?

    If it's a char array, just add the offset for the start position you want to the source pointer you pass to strcpy.

    If it's a string, use substr().

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: copying a string

    I guess he means that the C-string number is like 123-456-7890 and he wants a copy without the -.
    Something like this maybe
    Code:
    char in[] = "123-456-7890";
    char out[sizeof(in)-2]={0};
    memcpy( &out[0], &in[0], 3 );
    memcpy( &out[3], &in[4], 3 );
    memcpy( &out[6], &in[8], 4 );
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured