473,672 Members | 2,627 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 10947
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

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

Similar topics

9
18424
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 word char *mystring "Jane and Peter and Tom and Cindy" char *delim = " and "; char *token; token = strtok(mystring, delim);
1
3325
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 for my first element but my subsequent printf's will return garbage. Can someone let me know what I am doing wrong. Thanks in advance. -jlewis
2
8704
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: ---------------------------------------------------------------- void CNEWSERIALNO::OnBInsert() { // TODO: Add your control notification handler code here
16
2036
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 <string.h> int main()
29
4206
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 into two other strings, example:
2
12057
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 ..but it seems a bit "bloated" as i need an extra array of same size of the original string
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", "passwort" from "users" order by "users"
4
1422
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"); scanf("%s",&buf); token=strtok(buf," \n"); while(token!=NULL) {i++;
2
3403
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
8418
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8943
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8635
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8695
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7465
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5719
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4238
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2087
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1834
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.