2

I am splitting string through strtok but something different happen e.g

 *tok =strtok(string,"\r\n");

strtok when found only \r then it split string not whole \r\n , I want string should be splited only when whole string occur in data \r\n ;

T Percival
  • 8,526
  • 3
  • 43
  • 43
Mustafa Khan
  • 27
  • 1
  • 5

4 Answers4

2

To split a string using multiple character you can use strstr in a loop

Just repeatedly call strstr to find occurrences of your boundary string.

After you get a result, advance the pointer by the length of the substring and repeat the search again.

This is an ugly example but I think it does what you need

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

int main ()
{
  char buffer[128];
  char *str ="This is a long text \r\n with \r\nmany carriage returns,\
  I hate \r\n just this\r\n nothing else\r\n";

  char *token="\r\n";

  int l=strlen(str);
  char *start,*p;
  start=str;

  p= strstr (str,token);
  while(p)
  {
     strncpy( buffer,str, p-str);
     buffer[p-str] ='\0';

     printf("%s\n",buffer);

     str=p+strlen(token);

     if((str-start) >= l)
        break;

     p= strstr (str,token);
 }
 if(!p)
   printf("%s\n",str);
 return 0;
}

See HERE

PS:If anybody want, please edit the code, to make it bit better.

P0W
  • 46,614
  • 9
  • 72
  • 119
  • well its good example . upto 70% my problem is solved . but can you please check it with this string and then see behaviour of the program ."This is a long \r text \r\n with \r\n many carriage returns,\ I hate \r\n just this\r\n nothing else\r\n"; – Mustafa Khan Sep 10 '13 at 19:56
  • If I understood your question, I believe 100% of your problem is solved. Having just run the code I posted a few hours ago replacing all "ggg" with "\r\n", I produced the image I will edit the post with above. – ryyker Sep 10 '13 at 23:27
1

If you want to split a string based on the occurrence of a substring (such as "\r\n") you will have to use string search functions such as char * strstr(,). strstr() will return a pointer to the matched sub-string. You can then then manipulate the resulting string at that point to remove strlen("\r\n") bytes from the resultant, and continue your search.

[EDITED] from an excellent post here with a reference to excellent code by this guy

m-qayyums code is called in a loop to show you how you can look for (and replace) multiple occurrences of a sub-string with another sub-string, even an empty string using strstr(,) et. al.

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

char *replace_str(char *str, char *orig, char *rep);

int main ()
{
    char origStr[]={"this is anggg originalggg lineggg ogggf texgggt"};
    char newString[]={"this is anggg originalggg lineggg ogggf texgggt"};


    sprintf(newString, "%s", origStr);
    while(strstr(newString, "ggg"))
    {
        sprintf(newString, "%s", replace_str(newString, "ggg", ""));
    }
    printf("Original String:  %s\n", origStr);
    printf("New      String:  %s\n", newString);

    getchar();

    return 0;
}


char *replace_str(char *str, char *orig, char *rep)
{
  static char buffer[4096];
  char *p;

  if(!(p = strstr(str, orig)))  // Is 'orig' even in 'str'?
    return str;

  strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
  buffer[p-str] = '\0';

  sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));

  return buffer;
}

[EDIT_2] replacing "ggg" with "\r\n" I got this result using the code above:

enter image description here

Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

If you're on a *nix system, you could use the POSIX Regex library to achieve this result, as described here.

Community
  • 1
  • 1
Mauren
  • 1,955
  • 2
  • 18
  • 28
-1
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}

Output:

Splitting string "- This, a sample string." into tokens:
This
a
sample
string
jandresrodriguez
  • 794
  • 7
  • 18
  • thanks man . can yo please tell about any possiblility so that string split on the occurance of the whole string .??? i realy need this – Mustafa Khan Sep 10 '13 at 18:20
  • @P0W - Plagiarism is the highest form of flattery. And it is illustrative, (kind of) relevant, and salient. _(Kind of)_ because it really does not answer the question of how to break a string at every occurrence of a ***sub-string***. – ryyker Sep 10 '13 at 18:32