Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Partial String Matching
kvarnerexpress
post Oct 9 2005, 04:04 PM
Post #1


Super Member
*********

Group: Members
Posts: 407
Joined: 13-December 04
Member No.: 2,696



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:
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
Go to the top of the page
 
+Quote Post
dexter
post Oct 10 2005, 12:45 AM
Post #2


Advanced Member
*******

Group: Members
Posts: 142
Joined: 24-December 04
From: Queensland, Australia
Member No.: 2,902



The problem is the way you constructed your if statement.

This:
CODE

// 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:

CODE

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:

CODE

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.

Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. What Is A T_string?(13)
  2. Determining Whether A Phrase Is In A Variable(2)
  3. Building My First Instrument(6)
  4. Matching Accents In Perl Regular Expresions(1)
  5. Can I Cast A String As A Class Object?(7)
  6. Partial Birth Abortion(13)
  7. Request Script: Short Or Full View Without Sql(2)
  8. Various String Functions In C(5)
  9. String Search Outside My Site?(2)
  10. File String Delete?(2)
  11. Not Allowing Symbols In A String(3)
  12. Java String Overview(2)
  13. Insert String To Mysql(5)
  14. Mysql For Vb.net(2)
  15. String/text Formatting?(4)
  1. String Theory And The Ten Dimensions Of The Universe(10)
  2. How To Customize Your Ringtone W/matching Avatar(0)
  3. Incrementing Mysql Integer(7)
  4. Help Php: How To Load String From Text File (solved)(9)
  5. <?php ?> Sloppy Login Script(12)
  6. T_string Error Please Assist(5)
  7. Data Structures -- String -- Palindrome(5)
  8. Data Structures -- String -- Arrange Based On Repetition(1)
  9. Adding String To Integer?(1)
  10. Barack Obama, Baby Killer?(15)
  11. Unexpected T_string In User.php [resolved](5)


 



- Lo-Fi Version Time is now: 11th October 2008 - 11:59 PM