Thread: copying a characters from a string

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    118

    copying a characters from a string

    Look for a way that i can take a number of characters in a string and store it in another.

    ie. the string "Hello World", how can i store the part that says "world" in a string, i already found out the number of characters i want copied but i need to now know how to copy them

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Assuming by 'string' you mean the standard char array, you could use memcpy().

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by root4 View Post
    Assuming by 'string' you mean the standard char array, you could use memcpy().
    Or more appropriately, strcpy().

    You just need to find the start of your substring, and then do something like:
    Code:
    strcopy(sub, &orig[pos]);

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    118
    yes, that what i meant...thank you
    ill be using this alot so ill be trying both but they seem to work thanks

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    Or more appropriately, strcpy().
    Yes, of course, that's what I meant, sorry for the memcpy() (even if it works...)

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by root4 View Post
    Yes, of course, that's what I meant, sorry for the memcpy() (even if it works...)
    Sure it works. I've seen C-libraries that do:
    Code:
    char *strcpy(char *dest, const char *src)
    {
        memcpy(dest, src, strlen(src)+1);
        return dest;
    }
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Replies: 4
    Last Post: 10-14-2005, 12:53 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM