Thread: splitting strings...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    5

    splitting strings...

    this has been puzzling me for some time now, i've heard that it can be done with strtok() but i cant get my head around it....

    basically i want to split a 4 character string i.e. 'ABCD' in half, and store the results into two different variables. so, if the string x is equal to 'ABCD', then i want to make y = 'AB' and z = 'CD'. what is the easiest way to do this?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strncpy
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Simplest code to solve exactly the problem you describe:
    Code:
    char x[] = "ABCD";
    char y[3];
    char z[3];
    
    y[0] = x[0];
    y[1] = x[1];
    y[2] = 0;
    
    z[0] = x[2];
    z[1] = x[3];
    z[2] = 0;
    However, I expect that you don't always have 4-letter strings that you want to make into two-letter strings, and then the problem gets a bit hairier.

    --
    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
    Aug 2008
    Posts
    5
    matsp, the strings are always 4 characters long but sometimes they may contain numbers although I dont think this would matter in the example you showed me before. thanks.

    vart, could you possibly elaborate? i'm aware that strncpy() copies no more than a set number of bytes, but how would this apply to my question?

    cheers guys.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by newbie_socketsp View Post
    vart, could you possibly elaborate? i'm aware that strncpy() copies no more than a set number of bytes, but how would this apply to my question?
    you can ask it to copy 2 bytes from the original string...
    but if it is always 2 bytes - using Mats's method is better
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. splitting up names (strings)
    By Butters007 in forum C Programming
    Replies: 8
    Last Post: 12-07-2006, 01:13 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Splitting strings into words.
    By 0x7f in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2003, 03:49 PM
  5. splitting strings
    By Unreg1 in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2002, 11:02 PM