1

I am trying to write a program in C, which will allow me to get a string I want between two other strings that will be defined. To be more specific, my example string is

 "blahblah<mailto:[email protected]>blahblahblah"

and I need to be able to extract the "agent007" substring to a new variable. I have tried the strtok() method, but the thing is I can't extract the tokens to a new variable or an array. I have tokenized the string and the statement that would suite me fine would be something like " if token[i] == "mailto" && token[i+2] == "example" then mailAdd = token[i+1] " (in a pseudo-code way :) )

my program so far

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

int main ()
{
  char str[] ="blahblah<mailto:[email protected]>blahblahblah";
  char * tch;
  tch = strtok (str,"<:@");
  while (tch != NULL)
  { 
    printf ("%s\n",tch);
    tch = strtok (NULL, "<:@");
  }
  return 0;
}

Of course, any other suggestion beyond tokens will be greatly appreciated -

5
  • Tokens seem reasonable to me. I'm not clear on what problem you're having. Why can't you extract the tokens? Commented Dec 14, 2010 at 15:40
  • Hello Jonathan. I can't seem to find a way to export the token to a char var and later on use it in the program for other calculations. Did I make sense ? :) Commented Dec 14, 2010 at 15:47
  • if you want to reuse it, you need to copy the tch string somewhere: tch itself keeps changing. Note that strtok changes the original string. After your loop, (if I didn't mess the calculations up) str+0 points to "blahblah"; str+9 points to "mailto"; str+16 points to "agent007"; and str+25 points to "example.org>blahblahblah". Commented Dec 14, 2010 at 16:01
  • @pmg:Thanks for your reply. But here's a tricky one : How will I be able to still get the "agent007" token without knowing beforehand the lenght of the string? :) Commented Dec 14, 2010 at 16:39
  • You just need to save the pointer, @Sakis. I'll update my answer below. Commented Dec 14, 2010 at 16:41

2 Answers 2

2

My first thought was to use strstr for "mailto:" and strchr for the '@'

// pseudo code
char *mailto = strstr(src, "mailto:"); // possibly convert src to lowercase
char *atsign = strchr(mailto, '@');
while (mailto < atsign) *dst++ = *mailto++;

Of course that is a very rough draft. It needs lots of refining (failure to find the "mailto:" string or '@' char, error-checking, special cases, tests, ...)


Saving the strtok pointer

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

int main ()
{
  char str[] ="blahblah<mailto:[email protected]>blahblahblah";
  char * tch;
  char * saved;                     /* pmg */
  tch = strtok (str,"<:@");
  while (tch != NULL)
  { 
    int savenext = 0;               /* pmg */
    if (!strcmp(tch, "mailto"))     /* pmg, UNTESTED CODE, need to check case? */
    {                               /* pmg */
      savenext = 1;                 /* pmg */
    }                               /* pmg */
    printf ("%s\n",tch);
    tch = strtok (NULL, "<:@");
    if (savenext == 1)              /* pmg, UNTESTED CODE */
    {                               /* pmg */
      saved = tch;                  /* pmg */
    }                               /* pmg */
  }
  printf ("saved: %s\n", saved);    /* pmg */
  return 0;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Well, I see what you did there :) Tested and working like a charm. Thanks a zillion pmg :)
1

You could use strstr to search for 'mailto:' and then strchr to search for '@' and take the characters in between. I never use strtok but I don't see what's wrong with what you've done.

Here's an example where email should point to "agent007" in your case. Error handling is missing here. This is destructive, meaning that it modifies the input string, but so does strtok.

char *mailto = strstr( str, "mailto:" );
char *at = strchr( mailto, '@' );
char *email = mailto + strlen("mailto:");
*at = '\0';

1 Comment

That IS however the problem I'm facing. :) How can I take those characters ?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.