cyd3922
cyd3922

Reputation: 1

tokenizing in c

I'm trying to tokenize a string using all non-letter characters as delimiters.

I have a line specifying this:

char delimiters[] = "1234567890-=!@#$%^&*()_+[]\;',./{}|:"<>?"

But im pretty sure this is not valid code. Any help is greatly appreciated. Thanks.

Upvotes: 0

Views: 131

Answers (2)

Ramsey Nasser
Ramsey Nasser

Reputation: 21

Tokenizing a string using the standard library can be done with strtok. It has a particular way it expects to be called: on the first call, you pass in your input string and delimiters and strtok returns the first token. After that, every time you call strtok with NULL and the delimiters as parameters you get a subsequent token.

Consult a reference for more information. The code example below is based on the one on the linked site.

And as it's been said, you have to escape the " and \ characters by using \" and \\.

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a sample string: [email protected]";
  const char delimiters[] = "1234567890-=!@#$%^&*()_+[]\\;',./{}|:\"<>?"
  char * pch;

  printf ("Splitting string \"%s\" into tokens:\n",str);

  pch = strtok (str, delimiters);
  while (pch != NULL)
  {
    // pch now point to a token in your string
    printf ("%s\n",pch);
    pch = strtok (NULL, delimiters);
  }
  return 0;
}

Upvotes: 2

titaniumdecoy
titaniumdecoy

Reputation: 19251

You need to escape the " and \ with a \:

const char delimiters[] = "1234567890-=!@#$%^&*()_+[]\\;',./{}|:\"<>?"

However, if you really are trying to match "all non-letter characters" (in which case your list of characters is woefully inadequate), you should use a built-in C function such as isalpha instead.

Upvotes: 1

Related Questions