0

I want to write a function, that deletes an entry of a structure list if it finds a substring in the name of the author saved in the structure. The problem is, that strstr() does not seem to find any match. I dont want to know if I am deleting the entries right or moving the list pointers in the right way (wanna figure that out myself).

void z(LIBRARY *first){
   int  i = 0, j = 0 ;
   LIBRARY *nextZ = first->next , *prevZ = first;
   char *name = (char*) malloc (102*sizeof(char)), *name2 = (char*) malloc (102*sizeof(char)), c ;

   getchar();

   fgets(name, 102, stdin); // Function gets the string the user is looking for.
   while (name[j]!= '\0'){
     name[j]= toupper(name[j]);  // Converts it to upper case to ignore case types.
     j++;
   }
   j=0;
   printf("%s", name);

   while ( nextZ ){ // Function starts going through the list of structures

     name2 = nextZ->authors; //Function gets string saved in current structure->authors
     while (name2[j]!= '\0'){
        name2[j]= toupper(name2[j]);
        j++;
     }
     j = 0;
     printf("%s", name2);

     if ( (strstr( name2, name))!=NULL ){ 
         /*This is where the problem is. It seems like   
         the function never finds a substring in the structure->authors entry.*/
        i++;
        prevZ->next = nextZ->next;
        nextZ = nextZ->next->next;
     }
     else {
        prevZ = prevZ->next;
        nextZ = nextZ->next;
     }

   }
 printf("Vymazalo sa %d zaznamov\n", i); //Prints out how many entries of the list got deleted.
}
user3501229
  • 13
  • 1
  • 3
  • 1. To learn how `strstr()` works, write a simple main which doesn't do anything else but playing with `strstr()`. 2. To understand what your program really does trace it step by step using a debugger inspecting all relevant variable on each step. – alk May 02 '14 at 12:53
  • 1
    Btw: The code introduces a big memory leak here: `name2 = nextZ->authors;` – alk May 02 '14 at 12:55
  • Idea: Change this `printf("%s", name2);` to be `printf("name2='%s', name='%s'\n", name2, name);` – alk May 02 '14 at 12:57
  • Hey thanks I figured out the main problem- its that I loaded the end of line character into name and thus it could not find that string. But what did you mean by big memory leak in name2 = nextZ->authors; ? – user3501229 May 02 '14 at 13:10
  • Have a look what you assigne when to the pointer `name2`. – alk May 02 '14 at 13:14
  • Thanks for help- I added an answer to this question where I posted how I resolved all the problems. – user3501229 May 03 '14 at 10:50

1 Answers1

0

I have managed to solve all of the problems I had- here is what I did for anyone interested:

void z(LIBRARY *first){
 int  i = 0, j = 0 ;
 LIBRARY *nextZ = first->next , *prevZ = first;
 char *name = (char*) malloc (102*sizeof(char)), *name2 = (char*) malloc (102*sizeof(char));

 getchar();  //getting an '\n' that could remain from main when calling z()

 fgets(name, 102, stdin);        //scanning author the user wants to delete
 while (name[j]!= '\0'){
     if (name[j]=='\n') {        //removing end of line character from user input
                 name[j]='\0';
                 break;
     }
     if (name[j]>='a' && name[j]<='z') name[j]= toupper(name[j]);        //chaning user choice to upper case letters for string comparison
     j++;
 }
 j=0;

 while ( nextZ ){    //While cycle that goes through the list of structures

     strcpy(name2, nextZ->authors);  //gets author name from current list entry
     while (name2[j]!= '\0'){

         if (name2[j]>='a' && name2[j]<='z') name2[j]= toupper(name2[j]);    //Once again- change to upper case for string comparison
         j++;
     }
     j = 0;

     if ( (strstr(name2,name))!=NULL ){      //strstr- returns a pointer if match is found- otherwise returns NULL
         i++;
         prevZ->next = nextZ->next; //removes the entries from the list
         nextZ = nextZ->next;

     }
     else { //if strings dont match the list moves to the next entry
         prevZ = prevZ->next; 
         nextZ = nextZ->next;
     }

 }
 printf("%d entries deleted.\n", i); //prints out number of deleted entries
}
user3501229
  • 13
  • 1
  • 3