Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to split a string and store it in two separate strings?

Status
Not open for further replies.

mformazhar1980

Junior Member level 1
Joined
Mar 23, 2009
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,436
hi
hi

i have an string value with a space in between like
str[]="abcd,dcba"

and i need to split this string and get it like that

str1[]="abcd"

str2[]="dcba"

i have checked the "strtok" but i dont know how to store the resulting split ed string into str1[], and str2[] can any body help me out for this thanks
 

arthur0

Full Member level 2
Joined
Nov 28, 2003
Messages
131
Helped
49
Reputation
98
Reaction score
36
Trophy points
1,308
Location
Stockholm, Sweden
Activity points
1,218
strtok repeatedly

Hi,

Normally you call strtok repeatedly until it returns NULL.
In your case, when you call it the first time it will return a pointer to a null-terminated string beginning at str[0] (‘a’) and terminated at str[3] (‘d’, with a ‘\0’ instead of comma at str[4]).
The second time you call it, it will return a pointer to a null-terminated string beginning at str[5] (‘d’) and terminated at str[8] (‘a’, with a ‘\0’ at str[9]).
Note that you don't need to allocate and/or store the resulting strings, because they can be used "in-place" (stored in str[]'s space).

So, in your particular example, you can write:
Code:
str1 = strtok(str, ",");	 /* will give str1 = "abcd" */
str2 = strtok(NULL, ",");	/* will give str2 = "dcba" */
Generally, you would call strtok in a loop like this:
Code:
token = strtok( string, seps );

while(token != NULL)
{/* while there are tokens in "string" do...*/
      /* do something with the token here*/
      /* ... */
      /* get next token: */
      token = strtok(NULL, seps);
}
Arthur
 

mformazhar1980

Junior Member level 1
Joined
Mar 23, 2009
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,436
string problem

it gives me error if i do that, can u plz tell me where i am making mistake thanks

#include <stdio.h>
#include <string.h>
int main ()
{
char str1[5],str2[5];
char str[] ="abcd,dcba";
char * token;
printf ("Splitting string \"%s\" into tokens:\n",str);
token = strtok (str,",");
str1=strtok (str,",");
while (token != NULL)
{
printf ("%s\n",token);
token = strtok (NULL, ",");
str2=strtok (NULL, ",");
}
return 0;
}
 

arthur0

Full Member level 2
Joined
Nov 28, 2003
Messages
131
Helped
49
Reputation
98
Reaction score
36
Trophy points
1,308
Location
Stockholm, Sweden
Activity points
1,218
Re: string problem

Hi,
You didn't pay much attention to what I said in my previous post.
You didn't say what the error is you get either.

Anyway, your code should look like this:
Code:
#include <stdio.h> 
#include <string.h> 
#define ARR_SIZE   2

int main () 
{ 
	char str[] = "abcd,dcba"; 
	char *token;
	char *str[ARR_SIZE];	/* array of pointers to char, instead of str1, str2 */
	int idx = 0;	/* index in the array of pointers above */
	
	printf ("Splitting string \"%s\" into tokens:\n",str); 
	token = strtok (str,","); 
	
	while (token != NULL && idx<ARR_SIZE) 
	{ 
		str[idx] = token;
		printf ("%s\n",str[idx]); 
		token = strtok (NULL, ","); 
		idx++;
	} 
	
	return 0; 
}
The main differences from what you wrote are that:

1) I didn't allocate space for the resulting strings since the tokenization is done in-place. Instead I declared an array of pointers that will point to the resulting strings (this way you can easily scale up the number of tokens by just increasing the size of the array).
If you allocate strings like you did, you have to also provide a mechanism to copy the characters from the token to your allocated string.

2) The number of calls to strtok is very important since its implementation keeps an internal static variable that holds the progress of the tokenization.
Every time you call it with NULL, the tokenization process will advance with one token in the string until it hits the null character ('\0').
When you call it with a string, it resets this progress (i.e., its internal static variable), so previous tokenizations are interrupted.
This is very important to keep in mind when your using the function in different places in your program!

Arthur
 

mformazhar1980

Junior Member level 1
Joined
Mar 23, 2009
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,436
Re: string problem

hi aurther thanks for the reply i have tried the snippet u suggested but it is giving me following errors (see in attached file), and also i wanna to make it to u more clear that why i want to store the split ed string into str1[] and str2[] is because i need it in my program somewhere to convert the string to integer (in case we got numbers in the str[]), the idea is that the str[] has a separator(i.e.",") after 4 alphabets so in case if the string appears str[]= "1234,4321"; i should be able to split it and covert it into integer value(atoi), what do u think? is it the right way or is there any other way to perform such operation?
hope u get a clear picture of my problem.

looking forward to ur kind suggestion
 

arthur0

Full Member level 2
Joined
Nov 28, 2003
Messages
131
Helped
49
Reputation
98
Reaction score
36
Trophy points
1,308
Location
Stockholm, Sweden
Activity points
1,218
Re: string problem

Oh yes, I used the same name ("str") for both variables, my bad!
Replace your main() with:
Code:
int main () 
{ 
   char str[] = "abcd,dcba"; 
   char *token; 
   char *str_arr[ARR_SIZE];   /* array of pointers to char, instead of str1, str2 */ 
   int idx = 0;   /* index in the array of pointers above */ 
    
   printf ("Splitting string \"%s\" into tokens:\n",str); 
   token = strtok (str,","); 
    
   while (token != NULL && idx<ARR_SIZE) 
   { 
      str_arr[idx] = token; 
      printf ("%s\n",str_arr[idx]); 
      token = strtok (NULL, ","); 
      idx++; 
   } 
    
   return 0; 
}
Please note that I didn't try the code, so you should be able to adapt to any error that might come up.
Also, note that what you want in str1 and str2 you'll find in str_arr[0] and str_arr[1], respectively, but these strings are using the same space as str, so if there's a chance you'll overwrite this space before you're finished using the strings, you should copy them (all chars, one by one) to a new location, but I really don't think you need to do that.

So, to summarize, str_arr[0] is pointing to "abcd" (str_arr[0][0] is 'a', str_arr[0][1] is 'b' and so on).

Arthur
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top