C - Split string with repeated delimiter char into 2 substrings
I'm making a very simple C program that simulates the export command, getting an input with fgets().
Input example:
KEY=VALUE
Has to be converted to:
setenv("KEY", "VALUE", 1);
That's easy to solve with something similar to this code:
key = strtok(aux, "=");
value = strtok(NULL, "=");
The problem comes when the user input a value that start with one or several equals =
characters. For example:
KEY===VALUE
This should be converted to:
setenv("KEY", "==VALUE", 1);
But with my current code it is converted to:
setenv("KEY", NULL, 1);
How I can solve this?
Thanks in advice.
c strtok
add a comment |
I'm making a very simple C program that simulates the export command, getting an input with fgets().
Input example:
KEY=VALUE
Has to be converted to:
setenv("KEY", "VALUE", 1);
That's easy to solve with something similar to this code:
key = strtok(aux, "=");
value = strtok(NULL, "=");
The problem comes when the user input a value that start with one or several equals =
characters. For example:
KEY===VALUE
This should be converted to:
setenv("KEY", "==VALUE", 1);
But with my current code it is converted to:
setenv("KEY", NULL, 1);
How I can solve this?
Thanks in advice.
c strtok
You have to write your own parser.
– john elemans
Nov 13 '18 at 19:11
4
You could just usestrchr
instead ofstrtok
– Andrew Sun
Nov 13 '18 at 19:11
Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a
– SergeyA
Nov 13 '18 at 19:13
add a comment |
I'm making a very simple C program that simulates the export command, getting an input with fgets().
Input example:
KEY=VALUE
Has to be converted to:
setenv("KEY", "VALUE", 1);
That's easy to solve with something similar to this code:
key = strtok(aux, "=");
value = strtok(NULL, "=");
The problem comes when the user input a value that start with one or several equals =
characters. For example:
KEY===VALUE
This should be converted to:
setenv("KEY", "==VALUE", 1);
But with my current code it is converted to:
setenv("KEY", NULL, 1);
How I can solve this?
Thanks in advice.
c strtok
I'm making a very simple C program that simulates the export command, getting an input with fgets().
Input example:
KEY=VALUE
Has to be converted to:
setenv("KEY", "VALUE", 1);
That's easy to solve with something similar to this code:
key = strtok(aux, "=");
value = strtok(NULL, "=");
The problem comes when the user input a value that start with one or several equals =
characters. For example:
KEY===VALUE
This should be converted to:
setenv("KEY", "==VALUE", 1);
But with my current code it is converted to:
setenv("KEY", NULL, 1);
How I can solve this?
Thanks in advice.
c strtok
c strtok
edited Nov 13 '18 at 19:07
Tony Ceralva
asked Nov 13 '18 at 19:05
Tony CeralvaTony Ceralva
7471720
7471720
You have to write your own parser.
– john elemans
Nov 13 '18 at 19:11
4
You could just usestrchr
instead ofstrtok
– Andrew Sun
Nov 13 '18 at 19:11
Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a
– SergeyA
Nov 13 '18 at 19:13
add a comment |
You have to write your own parser.
– john elemans
Nov 13 '18 at 19:11
4
You could just usestrchr
instead ofstrtok
– Andrew Sun
Nov 13 '18 at 19:11
Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a
– SergeyA
Nov 13 '18 at 19:13
You have to write your own parser.
– john elemans
Nov 13 '18 at 19:11
You have to write your own parser.
– john elemans
Nov 13 '18 at 19:11
4
4
You could just use
strchr
instead of strtok
– Andrew Sun
Nov 13 '18 at 19:11
You could just use
strchr
instead of strtok
– Andrew Sun
Nov 13 '18 at 19:11
Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a
– SergeyA
Nov 13 '18 at 19:13
Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a
– SergeyA
Nov 13 '18 at 19:13
add a comment |
2 Answers
2
active
oldest
votes
Your second strtok()
should not use =
as the delimiter. You would only do that if there were another =
that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.
key = strtok(aux, "=");
value = strtok(NULL, "");
Thanks! It works prefectly.
– Tony Ceralva
Nov 13 '18 at 19:43
add a comment |
strtok
is probably overkill (and non-reentrant) when it's just one token. This will do,
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
Although strtok
is probably easier to read. One may try strsep
, but it is GNU C.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287894%2fc-split-string-with-repeated-delimiter-char-into-2-substrings%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your second strtok()
should not use =
as the delimiter. You would only do that if there were another =
that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.
key = strtok(aux, "=");
value = strtok(NULL, "");
Thanks! It works prefectly.
– Tony Ceralva
Nov 13 '18 at 19:43
add a comment |
Your second strtok()
should not use =
as the delimiter. You would only do that if there were another =
that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.
key = strtok(aux, "=");
value = strtok(NULL, "");
Thanks! It works prefectly.
– Tony Ceralva
Nov 13 '18 at 19:43
add a comment |
Your second strtok()
should not use =
as the delimiter. You would only do that if there were another =
that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.
key = strtok(aux, "=");
value = strtok(NULL, "");
Your second strtok()
should not use =
as the delimiter. You would only do that if there were another =
that ended the value. But the value ends at the end of the string. Use an empty delimiter for this part.
key = strtok(aux, "=");
value = strtok(NULL, "");
answered Nov 13 '18 at 19:24
BarmarBarmar
424k35248349
424k35248349
Thanks! It works prefectly.
– Tony Ceralva
Nov 13 '18 at 19:43
add a comment |
Thanks! It works prefectly.
– Tony Ceralva
Nov 13 '18 at 19:43
Thanks! It works prefectly.
– Tony Ceralva
Nov 13 '18 at 19:43
Thanks! It works prefectly.
– Tony Ceralva
Nov 13 '18 at 19:43
add a comment |
strtok
is probably overkill (and non-reentrant) when it's just one token. This will do,
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
Although strtok
is probably easier to read. One may try strsep
, but it is GNU C.
add a comment |
strtok
is probably overkill (and non-reentrant) when it's just one token. This will do,
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
Although strtok
is probably easier to read. One may try strsep
, but it is GNU C.
add a comment |
strtok
is probably overkill (and non-reentrant) when it's just one token. This will do,
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
Although strtok
is probably easier to read. One may try strsep
, but it is GNU C.
strtok
is probably overkill (and non-reentrant) when it's just one token. This will do,
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
Although strtok
is probably easier to read. One may try strsep
, but it is GNU C.
answered Nov 13 '18 at 19:44
Neil EdelmanNeil Edelman
42238
42238
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287894%2fc-split-string-with-repeated-delimiter-char-into-2-substrings%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You have to write your own parser.
– john elemans
Nov 13 '18 at 19:11
4
You could just use
strchr
instead ofstrtok
– Andrew Sun
Nov 13 '18 at 19:11
Can't reproduce. coliru.stacked-crooked.com/a/913d3de1fdad058a
– SergeyA
Nov 13 '18 at 19:13