0

I'm kinda new in C so i need help. I need to split a string to string array with the delimiter "&&", the thing is that i tried to use strtok, but when i use it, it looks like that the strtok can't handle correctly if the is '&' some where in the string.

I know that each part between the && is at max 256 chars and that there are at most 16 parts. so i need to create an array arr[16][256] or an array of size 16*256. in any case each time i try my code fail on the different between & and &&

example:

    char arr[16][255];
    char stringToSplit = "Hello World && How are u doing && more words & bla &";

output:

    arr[0] = "Hello World ";
    arr[1] = " How are u doing ";
    arr[2] = " more words & bla &";

Thanks ahead!

This is what i tried:

  int i;
  char *p;
  i = 0;
  p = strtok (stringToSplit ,"&&");
  while (p != NULL)
  {
    arr[i++] = p;
    p = strtok (NULL, "&&");
  }
  for (i=0;i<16; ++i)
    printf("%s\n", arr[i]);
6
  • 3
    strtok doesn't handle this because you are passing stringToSplit that pointing to string literal and strtok modifies string to which you pass. Read this answer . Apr 28, 2014 at 18:34
  • 1
    Show us what you tried
    – P0W
    Apr 28, 2014 at 18:34
  • the *string was worng... look at the edit :)
    – tubu13
    Apr 28, 2014 at 18:37
  • 2
    Another problem is that strtok only tokenizes using any single character within the given delimiter string. So passing it "&&" is no different than passing it "&". You may need to write your own tokenizing function, which is not that difficult.
    – ooga
    Apr 28, 2014 at 18:40
  • check this or this Apr 28, 2014 at 19:00

3 Answers 3

0

strtok() splits the string on characters.

You need strstr() to find a string inside a string then copy the relevant characters to the destination. Keep doing this until the source string is exhausted.

4
  • I tried that and it worked. but the problem was that I got some unknown chars at the end of each index. the arr[0] was "Hello world @H"
    – tubu13
    Apr 28, 2014 at 18:47
  • 2
    You need to properly terminate all strings by writing '\0' at the last position
    – pmg
    Apr 28, 2014 at 18:48
  • how do i find the last index? the strlen() doesnt give the right length of the string...
    – tubu13
    Apr 28, 2014 at 18:49
  • The last index is the (relative) position of the next occurrence of "&&".
    – pmg
    Apr 29, 2014 at 8:09
0

A simple double nested loop will do. check for "&&" and watch out for long sub-strings and too many sub-strings.

void  tubu13(void) {
  #define NUM (16)
  #define SIZ (255+1)

  char arr[NUM][SIZ];
  const char *stringToSplit = "Hello World && How are u doing && more words & bla &";

  const char *p = stringToSplit;
  int n, len;
  for (n = 0; n < NUM; n++) {
    for (len=0; len < SIZ-1; len++) {
      if (p[0] == '\0') {
        break;
      }
      if (p[0] == '&' && p[1] == '&') {
        p += 2;
        break;
      }
      arr[n][len] = *p;
      p++;
    }
    arr[n][len] = '\0';
    printf("  arr[%d] = \"%s\";\n", n, arr[n]);
  }
}

arr[0] = "Hello World ";
arr[1] = " How are u doing ";
arr[2] = " more words & bla &";
arr[3] = "";
...
arr[15] = "";
0
#include <stdio.h>
#include <string.h>

char *strtokByWord_r(char *str, const char *word, char **store){
    char *p, *ret;
    if(str != NULL){
        *store = str;
    }
    if(*store == NULL) return NULL;
    p = strstr(ret=*store, word);
    if(p){
        *p='\0';
        *store = p + strlen(word);
    } else {
        *store = NULL;
    }
    return ret;
}
char *strtokByWord(char *str, const char *word){
    static char *store = NULL;
    return strtokByWord_r(str, word, &store);
}

int main(){
    char arr[16][255];
    char stringToSplit[] = "Hello World && How are u doing && more words & bla &";
    int i, n;
    char *p;
    i = 0;
    p = strtokByWord(stringToSplit, "&&");
    while (p != NULL){
        strcpy(arr[i++], p);
        p = strtokByWord(NULL, "&&");
    }
    n = i;
    for (i=0;i<n; ++i)
        printf("%s\n", arr[i]);
    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.