splitting strings...

This is a discussion on splitting strings... within the C Programming forums, part of the General Programming Boards category; this has been puzzling me for some time now, i've heard that it can be done with strtok() but i ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    strncpy
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 12:13 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Splitting strings into words.
    By 0x7f in forum C++ Programming
    Replies: 6
    Last Post: 03-31-2003, 02:49 PM
  5. splitting strings
    By Unreg1 in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2002, 10:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21