Solved

How to split a string and put its element in a array?

Posted on 2007-04-03
6
391 Views
Last Modified: 2010-08-05
I'd like to split a string and put its element into an array.For example, I have a string
ls -l arg
and an array
char *arg[]
I'd like to split that string and assign each substring for elements of the array.The expected result is
arg[0]= ls;
arg[1]= -l;
arg[2]= arg
I use strktok function with the delimiter is space character but it seems did not work. Do i have to malloc for the array because we do not know the number of substrings?
0
Comment
Question by:khoama
  • 4
  • 2
6 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 18841798
strtok will work just fine. Either you do it like this :

  char str[] = "ls -l arg";
  char *str_ptr;
  char *arg[3] = { 0 };
  int count = 0;
  str_ptr = strtok(str, " ");
  while (str_ptr) {
    arg[count++] = str_ptr;
    str_ptr = strtok(NULL, " ");
  }

And then you have to realise that the arg array's elements point INTO the original str like this :

    "ls\0-l\0arg"
     ^   ^   ^
      |    |    arg[2]
      |    arg[1]
      arg[0]

If you don't want that, you'll have to COPY the tokens :

  char str[] = "ls -l arg";
  char *str_ptr;
  char *arg[3] = { 0 };
  int count = 0;
  str_ptr = strtok(str, " ");
  while (str_ptr) {
    arg[count] = malloc(strlen(str_ptr) + 1);
    strcpy(arg[count++], str_ptr);
    str_ptr = strtok(NULL, " ");
  }
  /* don't forget to free the arg elements !! */
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18841799
Note that in both cases, the original string is modified (every space is replaced by a NULL character) !!

http://www.cplusplus.com/reference/clibrary/cstring/strtok.html
0
 
LVL 2

Author Comment

by:khoama
ID: 18842182
thanks for your reply.Can you show me a full, easy-to-understand, source about String,Pointer,Array (multi-dimension),I/O,FILE (how to manipulate a file) in C? I understand them but still ambiguous  
0
Make managing Office 365 email signatures a breeze

Are you using Office 365? Having trouble trying to set up email signatures for your users? Getting stressed out managing multiple signatures? Need an easier way to manage? We have a solution for you, try the most-user friendly and powerful signature management tool on the market.

 
LVL 53

Expert Comment

by:Infinity08
ID: 18842183
Why a B grade ? Is there something that wasn't clear ? If so, just ask for clarification ...

You don't have to accept the solution if you're not happy with it !!
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18842195
>> Can you show me a full, easy-to-understand, source about String,Pointer,Array (multi-dimension),I/O,FILE (how to manipulate a file) in C?

This is not something that can easily be answered. I suggest reading up on these topics. If you have specific questions, you can always ask them here, and we'll be glad to answer them for you !


This is a good tutorial :

http://www.cplusplus.com/doc/tutorial/

It's for C++, but the chapters about pointers and arrays are valid for C too.


For C I/O, check the reference :

http://www.cplusplus.com/reference/clibrary/cstdio/

It contains explanations for each function, including examples showing how to use it.
0
 
LVL 2

Author Comment

by:khoama
ID: 18843795
hi guy please do not misunderstand me,Your answer is clear.Thanks for your comments
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Passing a array as parameter - C 2 84
Problem to show menu 5 87
How to creat good software interface ? 1 60
How to access ANSI/IEEE Std 754 or equivalent information ? 3 46
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now