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.
10 10947
Probably this link may help you..... STRTOK Link
Regards
Dheeraj Joshi
I know abt this but it does not split it '\' strtok. Even i cannot compile it.
The link was given for your reference not to copy the code from the that page.
Regards
Dheeraj Joshi
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.
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 - #include <iostream>
-
#include <string.h>
-
using namespace std;
-
-
int main()
-
{
-
char str[] = "now # is the time for all # good men to come to the # aid of their country";
-
char delims[] = "\"; //// error because of this
-
char *result = NULL;
-
result = strtok( str, delims );
-
while( result != NULL ) {
-
printf( "result is \"%s\"\n", result );
-
result = strtok( NULL, delims );
-
}
-
return 0;
-
}
-
~
You need to escape backslash in character string with backslash like you do it with quote five lines below.
newb16 is, I think, pointing out that there is a clash between the delimiters in the string ("#") and the 'char delims []' declaration ("/") - char *p=strtok(str," / "); /*This call is required to
-
identify the string you want to analyze as the s1 argument.
-
Now loop to analyze the string and print out the tokens
-
(words)*/
-
while (p){
-
cout<<p<<'\n';
-
p= strtok(NULL,"/ ");
-
}
I haven't compiled or debugged this but this is my understanding of how the function works.
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:
@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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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);
|
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
|
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
|
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()
|
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:
| |
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
|
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"
|
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++;
|
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;
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |