#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    81
    Rep Power
    12

    converting a string into char*[]


    Okay I have a string that has white spaces and I need to change it so that it is char pointer to an array of characters.

    string mine ="first next last";

    how do I change it so that it becomes

    char *temp[] ={"first","second",(char*)0};

    Thanks
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    In other words, tokenize the line using whitespace as a delimiter.

    I would use strtok(). Others recommend sscanf(). To use sscanf, I think you would need to know at compile time how many tokens you expect. OTOH, strtok is used with multiple calls, so it can handle a variable number of tokens.

    Hope that's what you were asking for.
  4. #3
  5. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    what about: step through the string to ascertain how many seperate strings are needed, then use something along the lines on this page http://www.eskimo.com/~scs/C-faq/q6.16.html to create the arrays then step through the original string again inserting them?

    or what about simply stepping through replacing all the ' ' with '\0' ? i guess that's not a propper multidemensional array though? not sure. there's something about multidemnsional array simulation on that page aswell.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    Originally posted by balance
    ...
    or what about simply stepping through replacing all the ' ' with '\0' ? i guess that's not a propper multidemensional array though? not sure. there's something about multidemnsional array simulation on that page aswell.
    Actually, that's what strtok does. Searches for the next delimiter character, replaces it with a '\0', and returns a char* to the start of that string. Then subsequent calls do the same for the next substring and so on.

    One thing that is easy to overlook about strtok, though: it modifies the string that you give it. So if you need to have that string back in one piece with you're done tokenizing it, you'd better make a copy of it and feed that through strtok.
  8. #5
  9. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    oh right - didn't realise that.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0
    Im having problems with the implementation of this, can anyone lend a hand?

    Code:
    char *cmd = "", *cmd1 = "", *cmd2 = "";
    	
    cout << "\nCMD> "; //Command Prompt
    cin.get(cmd, 20);
    	
    cmd1 = strtok(cmd," "); //How do i get strtok to go to the next token?
    cmd2 = strtok(cmd," ");
    	
    cout <<cmd1<<"\n"<<cmd2;
    If my input were:

    type char

    The output should be:

    type
    char

    How can I do this?
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    3
    Rep Power
    0
    Originally posted by dirtgreg
    Im having problems with the implementation of this, can anyone lend a hand?

    Code:
    char *cmd = "", *cmd1 = "", *cmd2 = "";
    	
    cout << "\nCMD> "; //Command Prompt
    cin.get(cmd, 20);
    	
    cmd1 = strtok(cmd," "); //How do i get strtok to go to the next token?
    cmd2 = strtok(cmd," ");
    	
    cout <<cmd1<<"\n"<<cmd2;
    If my input were:

    type char

    The output should be:

    type
    char

    How can I do this?
    Nevermind, I got it:

    Code:
    char *cmd = "", *cmd1 = "", *cmd2 = "";
    	
    cout << "\nCMD> "; //Command Prompt
    cin.get(cmd, 20);
    	
    cmd1 = strtok(cmd," "); //How do i get strtok to go to the next token?
    cmd2 = strtok(NULL,"\0");
    	
    cout <<cmd1<<"\n"<<cmd2;
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    Here is an example of how to use strtok():
    Code:
    #include<iostream>
    #include<cstring>
    
    using namespace std; 	
    
    int main()
    {
    	char data[] = "type char";
    	char* pieces[20]={0};
    	
    	char seps[]=" ";
    
    	int i = 0;
    	pieces[i]=strtok(data, seps);
    	
    	while(pieces[i] != NULL)
    	{
    		i++;
    
    		//After a first call to strtok, the function may
    		//be called with NULL as string parameter, 
    		//and it will follow from where the last call
    		//to strtok found a delimiter.
    
    		pieces[i]=strtok(NULL, " ");//
    	}
    
    	for(int j=0; j<i; j++)
    		cout<<pieces[j]<<endl;
    
    	return 0;
    }
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2002
    Location
    Flint, MI
    Posts
    328
    Rep Power
    13
    If your libc has it, you should also look at strsep. Unfortunately it's missing from all SysV versions of libc, which means Linux and Solaris, the two biggest UNIX platforms out there. I don't know about the C implementations for Windows.

    You can find the source for the function in the *BSD source code though. It's incredibly useful for tokenizing a string. In fact the *BSD man pages provide an example.
    Clay Dowling
    Lazarus Notes
    Articles and commentary on web development
    http://www.lazarusid.com/notes/

IMN logo majestic logo threadwatch logo seochat tools logo