C语言 删除基于子字符串find的列表项



我想写一个函数,如果它在结构中保存的作者的名字中找到子字符串,则删除结构列表的条目。问题是,strstr()似乎没有找到任何匹配。我不想知道我是否正确地删除了条目,或者以正确的方式移动了列表指针(我想自己弄清楚)。

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]!= ''){
     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]!= ''){
        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 zaznamovn", i); //Prints out how many entries of the list got deleted.
}

我已经设法解决了我遇到的所有问题-下面是我为感兴趣的人所做的:

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]!= ''){
     if (name[j]=='n') {        //removing end of line character from user input
                 name[j]='';
                 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]!= ''){
         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
}

最新更新