Connecting Tech Pros Worldwide Help | Site Map

How a c string can be splitted by token '\' using strtok?

Member
 
Join Date: Mar 2008
Posts: 101
#1: 2 Weeks Ago
How a c string can be splitted by token '\' using strtok?
Example:
Hello\world is one c-string. I need to split it into Hello and world. How this can be implemented? Thanks in advance.
dheerajjoshim's Avatar
Familiar Sight
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 251
#2: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


Probably this link may help you.....

STRTOK Link

Regards
Dheeraj Joshi
Member
 
Join Date: Mar 2008
Posts: 101
#3: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


I know abt this but it does not split it '\' strtok. Even i cannot compile it.
dheerajjoshim's Avatar
Familiar Sight
 
Join Date: Jul 2009
Location: Bangalore, INDIA
Posts: 251
#4: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


The link was given for your reference not to copy the code from the that page.

Regards
Dheeraj Joshi
Familiar Sight
 
Join Date: Jan 2007
Posts: 188
#5: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


strtok(s1,s2) returns the next token in s1 using the delimiters specified in s2.
The above link explains this in more detail.
Presumably in your example the' \' is the delimiter.
Member
 
Join Date: Mar 2008
Posts: 101
#6: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


Following is the error when "\" is used as delemiter for the example given by dhiraj.
check.cpp:8:21: warning: missing terminating " character
check.cpp:8: error: missing terminating " character
check.cpp: In function ‘int main()’:
check.cpp:9: error: expected primary-expression before ‘char’
check.cpp:9: error: expected ‘,’ or ‘;’ before ‘char’
check.cpp:10: error: ‘result’ was not declared in this scope


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     char str[] = "now # is the time for all # good men to come to the # aid of their country";
  8.     char delims[] = "\";             //// error because of this
  9.     char *result = NULL;
  10.     result = strtok( str, delims );
  11.     while( result != NULL ) {
  12.     printf( "result is \"%s\"\n", result );
  13.     result = strtok( NULL, delims );
  14.     }
  15.     return 0;
  16. }
  17.  
~
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 380
#7: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


You need to escape backslash in character string with backslash like you do it with quote five lines below.
Familiar Sight
 
Join Date: Jan 2007
Posts: 188
#8: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


newb16 is, I think, pointing out that there is a clash between the delimiters in the string ("#") and the 'char delims []' declaration ("/")

Expand|Select|Wrap|Line Numbers
  1. char *p=strtok(str," / "); /*This call is required to 
  2. identify the string you want to analyze as the s1 argument.                                                                                                                          
  3. Now loop to analyze the string and print out the tokens
  4.  (words)*/
  5. while (p){
  6.       cout<<p<<'\n'; 
  7.      p= strtok(NULL,"/ ");
I haven't compiled or debugged this but this is my understanding of how the function works.
Member
 
Join Date: Aug 2008
Posts: 121
#9: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


going back to the theory every one using C should know, the '\' character is used to excape other characters giving them special meaning.
for example:
'\n' means the new line character
'\t' the tab character
and so on....

since the '\' has a special meaning for C you must escape it to have the character '\'.
So if you need:
Expand|Select|Wrap|Line Numbers
  1. char delims[] = "\\";
  2.  
Familiar Sight
 
Join Date: Jan 2007
Posts: 188
#10: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


@Tassos Souris
What you say is generally true but not so in this case as s2 is any delimiter's place holder as part of the strtok() function
Markus's Avatar
Moderator
 
Join Date: Jun 2007
Location: York, England, with wolves.
Posts: 4,936
#11: 2 Weeks Ago

re: How a c string can be splitted by token '\' using strtok?


Quote:

Originally Posted by whodgson View Post

@Tassos Souris
What you say is generally true but not so in this case as s2 is any delimiter's place holder as part of the strtok() function

You have misread the OP's question: he wants to use the backslash (\) as a delimiter. However, in C, the backslash is an escape character. Ergo, the line char delims[] = "\"; causes an error because the backslash is escaping the closing quote. Tassos' solution would work.
Reply