All pastes #103678 Raw Edit

Untitled

public text v1 · immutable
#103678 ·published 2006-07-28 13:12 UTC
rendered paste body
#include <stdio.h>
#include <string.h>

main()
{
  char list[] = "1.2.3.4,5.6.7.8,9.0.1.2";
  char match1[] = "5.6.7.8";
  char match2[] = "6.5.7.8";

  if(matchit(match1,list)) printf("We have a winner! %s\n",match1);
  else printf("We are such a looser\n");

  if(matchit(match2,list)) printf("We have a winner! %s\n",match2);
  else printf("We are such a looser\n");




}



int matchit(char *needle,char *haystack)
{
  char *ptr,*str;
  char *strtok_r(char *str, const char *delim, char **saveptr);
  char *token;


  for (str = haystack; ; str = NULL) {
    if((token = strtok_r(str, "," , &ptr)) == NULL)
      return 0; // no tokens left

    if(!strcmp(needle,token))
      return 1;
  }

  return 0; // nothing matched - we really shouldnt get here
}