-1

I have a string "abc\asd\zxc\12as", I want to use strtok() to split the string based on \\ delimiter. But having a \\ is treated as escape character. How can we handle this situation?

Also how can I get only the 3rd word(eg. zxc) from strtok()?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 8
    why do you tag with coth C and C++? – Support Ukraine Jul 07 '21 at 12:16
  • 2
    `string s = R"(abc\asd\zxc\12as)";` – Eljay Jul 07 '21 at 12:16
  • related/dupe: https://stackoverflow.com/questions/10220401/rules-for-c-string-literals-escape-character – NathanOliver Jul 07 '21 at 12:17
  • 1
    Raw strings as shown by @Eljay or escaping the backslashes "\\"... – Aconcagua Jul 07 '21 at 12:21
  • 2
    In C, you cannot split a string literal using `strtok` because attempted modification of the storage for the string literal results in *undefined behavior*. However, you can use the string literal as an initializer for an array of `char` and split the string in that array using `strtok`. – Ian Abbott Jul 07 '21 at 12:28
  • In C++ exactly the same – solely that their type is `char const*` right from the start and you'd need to cast constness away to be able to provoke UB... – Aconcagua Jul 07 '21 at 12:31
  • Apart from: Third argument: `char *w; for(unsigned int i = 0; i < 3; ++i, data = nullptr) { w = strtok(data, "\\"); if(!w) { /* error handling; make sure to abort loop appropriately */ } } /* use w */` – Aconcagua Jul 07 '21 at 12:39
  • I highly recommend not using C-Style strings in C++. Also, the `strtok` cannot be used with a character string literal because `strtok` modifies its parameter string. – Thomas Matthews Jul 07 '21 at 16:49

3 Answers3

3

Inside string literals in C and C++, you can use the \ character to refer to other characters that you can't type nicely in code - for example \n is understood to be the line feed character.

So for example the c-string "Hello\nWorld" if printed to the console would appear as

Hello 
World

Because of this in general the \ character needs to be escaped itself. For your example, this means using \\ in places where you might expect to use \. For example, this string "Hello\\World\\again" in the C source file would print as "Hello\world\again"

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Elemental
  • 7,365
  • 2
  • 28
  • 33
0

as string, \a means escaped a, escaping is usefull when you have chars like , newline(\n), tabs etc

what you need to do is escape de \ i.e. replacing them with \\

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You just need to call something like strtok(string, "\\").

To get the third word, you'll have to call strtok three times:

#include <stdio.h>
#include <string.h>

int main()
{
    char string[] = "abc\\asd\\zxc\\12as";
    char *firstword = strtok(string, "\\");
    char *secondword = strtok(NULL, "\\");
    char *thirdword = strtok(NULL, "\\");
    printf("%s\n", thirdword);
}

Also, if you have any control over the data in this situation, you might want to choose a different delimiter character, since \ is going to be eternally inconvenient. Could you use | instead?

Steve Summit
  • 45,437
  • 7
  • 70
  • 103