473,513 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

151 New Member
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.
Nov 5 '09 #1
10 10932
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
Probably this link may help you.....

STRTOK Link

Regards
Dheeraj Joshi
Nov 5 '09 #2
Man4ish
151 New Member
I know abt this but it does not split it '\' strtok. Even i cannot compile it.
Nov 5 '09 #3
Dheeraj Joshi
1,123 Recognized Expert Top Contributor
The link was given for your reference not to copy the code from the that page.

Regards
Dheeraj Joshi
Nov 5 '09 #4
whodgson
542 Contributor
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.
Nov 5 '09 #5
Man4ish
151 New Member
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.  
~
Nov 5 '09 #6
newb16
687 Contributor
You need to escape backslash in character string with backslash like you do it with quote five lines below.
Nov 5 '09 #7
whodgson
542 Contributor
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.
Nov 7 '09 #8
Tassos Souris
152 New Member
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.  
Nov 7 '09 #9
whodgson
542 Contributor
@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
Nov 8 '09 #10
Markus
6,050 Recognized Expert Expert
@whodgson
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.
Nov 8 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

9
18408
by: Lans | last post by:
I have a string that I need to tokenize but I need to use a string token see example i am trying the following but strtok only uses characters as delimiters and I need to seperate bu a certain...
1
3309
by: john | last post by:
Relatively new to C coding, so any help would greatly be appreciated. I'm having problems try to return my string array from my parsing function. When I do a printf I am getting the correct value...
2
8694
by: bluebeta | last post by:
Hi, I am using embedded visual C++ 4.0 and I want to use function strtok to split string into array. my sample code is as below: ...
16
2018
by: Amit Gupta | last post by:
Hi - I get a seg-fault when I compile and run this simple program. (seg-fault in first call to strtok). Any clues? My gcc is "gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)" #include...
29
4175
by: Andrea | last post by:
I want to write a program that: char * strplit(char* str1, char *str2, char * stroriginal,int split_point) that take stroriginal and split in the split_point element of the string the string...
2
12048
by: Kelly B | last post by:
I tried to write a code which would reverse the order of words in a given string. I.e if Input String=The C Programming Language Output String=Language Programming C The Here is the code...
11
904
by: Lothar Behrens | last post by:
Hi, I have selected strtok to be used in my string replacement function. But I lost the last token, if there is one. This string would be replaced select "name", "vorname", "userid",...
4
1410
by: dhirenved | last post by:
hi, i have to use string tokenizing to isolate commands and the arguments that i have to execute using the execvp command. here is what i'm using char* token; char buf; printf("enter command");...
2
3384
by: mattmao | last post by:
Here is my code, just to test the strtok functionality: #include <stdio.h> #include <stdlib.h> #include <string.h> struct sampleClass { int num; char *text;
0
7267
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7391
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7120
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5697
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4754
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3247
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
809
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.