Thread: How to divide readline() input strings?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    22

    How to divide readline() input strings?

    How can I divide the input string of readline()? For example in cosole i typed "This is me".

    How can I divide this to

    readline() string 1 = This
    readline() string 2 = is
    readline() string 3 = me


    thanks

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Consider strtok.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    readline is not a standard function, so I don't know for certain what type of data it returns, but I assume it's a char array/string. If so, use sscanf with the "%s" modifier, which skips over white space, or use strtok (my preference). Google should have plenty of examples of both.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    22
    lets say I used strtok to divide this line of strings. how can i assign each string to a variable? like string1 = This, string2 = is, string3=me

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Code:
    char *pch; 
    char string[50]; 
    char first[10], second[10], third[10]; 
    strcpy( string, "this is me" ); 
    printf( "%s\n", string ); 
     
    pch = strtok( string, " " );
    strcpy( first, pch ); 
     
    pch = strtok( NULL, " " );
    strcpy( second, pch ); 
     
    pch = strtok( NULL, " " );
    strcpy( third, pch ); 
    
    printf( "%s\n%s\n%s\n", first, second, third ); 
    // ect
    Last edited by twomers; 12-03-2012 at 11:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cannot open include file: 'readline/readline.h'
    By arupsarkar in forum C++ Programming
    Replies: 2
    Last Post: 06-25-2010, 11:07 AM
  2. HELP with READLINE.H
    By HIT_Braga in forum C Programming
    Replies: 10
    Last Post: 04-01-2010, 11:56 AM
  3. ReadLine()...refreshers???
    By verbity in forum C++ Programming
    Replies: 2
    Last Post: 12-24-2008, 01:09 PM
  4. ReadLine to int
    By DaveHope in forum C# Programming
    Replies: 2
    Last Post: 01-27-2006, 10:19 AM
  5. Interacting with other programs with readline
    By Provost in forum C Programming
    Replies: 1
    Last Post: 08-13-2004, 06:24 AM