Reputation: 2847
Can someone please explaing why the string is not splitted in the following code
#include <stdio.h>
int main(void)
{
char name[] = "first:last";
char first[20], last[20];
sscanf(name, "%s:%s", first, last);
printf("first: %s, last: %s", first, last);
return 0;
}
The output is
first: first:last, last:
but it should be
first: first, last: last
Kindly check code here http://ideone.com/JDSTt
Upvotes: 2
Views: 4047
Reputation: 206689
You can use something like this:
sscanf(name, "%[^:]:%s", first, last);
:
is not whitespace, so a regular %s
will not consider it as a delimiter. See scanf
for more details.
(Edited demo: http://ideone.com/m4LVP)
Upvotes: 10
Reputation: 11379
See the scanf documentation about %s
type specifier:
String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).
Upvotes: 2