Jump to content
xisto Community
kvarnerexpress

Partial String Matching

Recommended Posts

Im trying to search a linked list to delete a record which is in a file that works. But im now trying to match only part of the string partial string matching.

Im using strstr but i cant seem to get it working i get too few arguments error. And i also would like to know if im on the right track as to how im coding it. Any hints and tips would be greatly appreciated thanks.

Code:

/***************************************************************************** Menu option #3: Delete Record* Allows the user to remove one or more records from the customer and/or* stock linked lists. Partial string matching is implemented.****************************************************************************/void deleteRecord(TennisStoreType* ts){  StockNodeType* curStock;  StockNodeType* prevStock;  CustomerNodeType*  curCust;  CustomerNodeType*  prevCust;  char inputString[DESCRIPTION_MAX + 1];  char *description;  char tmpsurname[SURNAME_MAX];  char *surname;  char answerInput;  char *firstName;  char *subDesc;  int finished;      do  {      printf("\n\nInput String (1-40 characters) ");      fgets(inputString, DESCRIPTION_MAX +2 , stdin);  /* check if the input is longer then the buffer (40 chars)     Validating input of 40 characters for string*/  if(inputString[strlen(inputString) -1] != '\n')  {       printf("\nProduct description too delete less then 40 chars!\n");       readRestOfLine();       /* flush the buffer */  }  if(inputString[0] == '\n')  {    printf("Returning back to main menu\n");    finished = FAILURE;  }  }while(inputString[strlen(inputString) -1 ] != '\n');  /* tokenize the data fields to be searched*/      description = strtok(inputString, "\n");  surname = strtok(inputString, "\n");    firstName = strtok(inputString, "\n");      curStock = ts -> headStock;  prevStock = NULL;  curCust = ts -> headCust;  prevCust = NULL;      printf("Are you sure you want to delete\n");        do  {      answerInput = fgetc(stdin);      if(answerInput == 'y')      {        printf("you pressed yes!\n");      }      else if(answerInput == 'n')      {        printf("You pressed no!\n");        return;              }  } while (answerInput != '\n');  strcpy(inputString, subDesc);    if(subDesc = strstr(("Slazenger Classic Racquet"),     ("Slazenger")) != NULL)  {     printf("found a match %s", inputString);  }  else  {     printf("no match!");  }      while((curStock != NULL) && ((curCust != NULL) &&       strcmp(inputString,curStock->description) &&       strcmp(inputString, curCust -> surname) &&       strcmp(inputString, curCust -> firstName)!= MIN_INPUT))  {       prevStock = curStock;       curStock = curStock->nextStock;       /* searching through the customer nodes       if not null keep getting the next node       for searching*/       prevCust = curCust;       curCust = curCust -> nextCust;  }         if ((curStock == NULL)  || (curCust == NULL))  {       printf("This item is not listed! \"%s\" .\n",              inputString);       printf("Try again\n");       return;  }  else if ((prevStock == NULL) || (prevCust == NULL))  {                ts->headStock = curStock->nextStock;         ts->headCust  = curCust -> nextCust;  }  else  {         prevStock->nextStock = curStock->nextStock;         prevCust ->nextCust = curCust -> nextCust;  }                 ts-> stockCount --;         ts-> customerCount--;         printf("\nTotal stock or customer after deletion: %d\n"                , ts -> stockCount);         printf("%s", inputString);                  }

Thanks,kvarnerexpress

Share this post


Link to post
Share on other sites

The problem is the way you constructed your if statement.

This:

// The parentheses are not needed around the strings.  // They do nothing, nor even make it more readable.if(subDesc = strstr("Slazenger Classic Racquet",  "Slazenger") != NULL)

Is exactly the same as saying:

if(subDesc = (strstr("Slazenger Classic Racquet",  "Slazenger") != NULL))

Currently it is trying to assign to subDesc whether or not the return of strstr is equal to NULL. This doesn't fly. This is because assignments have the last priority.

A correct way of constructing that assignment would be:

if((subDesc = strstr("Slazenger Classic Racquet",  "Slazenger")) != NULL)

Whenever you use assignment in an if statement -always- wrap the assignment part in parentheses before doing the comparison.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.