Hi there! I was just wondering how could I modify the strstr function in order to calculate the distance between the two strings. Supposing that we have the following code:

char *
str_find (buf, sub)
     register char *buf;
     register char *sub;
{
  register char *bp;
  register char *sp;

  if (!*sub)
    return buf;
  while (*buf)
    {
      bp = buf;
      sp = sub;
      do {
          if (!*sp)
            return buf;
      } while (*bp++ == *sp++);
      buf += 1;
    }
  return 0;
}

Thanks and greetings from Spain ;D

So who did you copy that code from?
That style of C has been obsolete for 20+ years.

commented: Yikes! :) +36

Distance between the two strings? Do you mean the Levenshtein distance or something else?

int distance = 0;
char str[] = "Hello World";
char *ptr = strstr("World");
if( ptr != NULL)
   distance = ptr - str;

I searched strstr.c in Google and I found that (old, it seems) code. I need to add this distance thing in a way that a variable counts the distance between the end of the first string til the end of the other string.

why do you need to write a new function for that? Just subtract the two pointers as shown in the code I posted. By "count the distance" probably means count the number of characters. Subtracting the two pointers returned by strtok() will do that.

I need that in order to decrypt Vigenere's code. I have to count the distance between the repetitions in a text. My code does find the repetitions but not the distance, and that's why do I want to know, because I've tried many possibilities but none worked.

what part of "Just subtract the two pointers as shown in the code [Dragon] posted" are you still confused about?

I need that in order to decrypt Vigenere's code. I have to count the distance between the repetitions in a text. My code does find the repetitions but not the distance, and that's why do I want to know, because I've tried many possibilities but none worked.

Give two strings as example and tell us what u want exactly:

str1="my name is xx yy xx"
str2="is xx";

here what exactly you need as your o/p???

and anything you need form this can be acheived by strtok() or strstr() by using some pointer arithmetic.

Thanks,
DP

For instance, I have the original string
char* aux = "AONL";

and I have a kind of text like that:
char* text = "AONLODUOFAONLTTHTTZTTL";

So the function I want is one that returns 9, because the distance btween the two repetitions is 9. Sthing like that, maybe I did not told you with the exact words, but that's mainly what I want to achieve.

Thank you.

try this

char *first = strstr(text, aux);
char *next = (first != NULL)?strstr(text+strlen(aux), aux): NULL;
unsigned long distance;
if (next > first)
distance = (unsigned long)next - (unsigned long)first;
else
// no 2 occurnaces to find distance

--------------\

For instance, I have the original string
char* aux = "AONL";

and I have a kind of text like that:
char* text = "AONLODUOFAONLTTHTTZTTL";

So the function I want is one that returns 9, because the distance btween the two repetitions is 9. Sthing like that, maybe I did not told you with the exact words, but that's mainly what I want to achieve.

Thank you.

1)use strtok to get the last token
2)find strlen() for the last token
here is your distance!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.