C 基于子字符串“查找”删除列表项
我想写一个函数,如果它在保存在结构中的作者的名字中找到一个子字符串,它将删除结构列表的一个条目。问题是strstr()似乎找不到任何匹配项。我不想知道我是正确删除条目还是以正确的方式移动列表指针(我想自己弄清楚)C 基于子字符串“查找”删除列表项,c,substring,strstr,C,Substring,Strstr,我想写一个函数,如果它在保存在结构中的作者的名字中找到一个子字符串,它将删除结构列表的一个条目。问题是strstr()似乎找不到任何匹配项。我不想知道我是正确删除条目还是以正确的方式移动列表指针(我想自己弄清楚) 我已经设法解决了我遇到的所有问题——以下是我为任何感兴趣的人所做的: void z(LIBRARY *first){ int i = 0, j = 0 ; LIBRARY *nextZ = first->next , *prevZ = first; char *name
我已经设法解决了我遇到的所有问题——以下是我为任何感兴趣的人所做的:
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
}
void z(库*第一个){
int i=0,j=0;
库*nextZ=first->next,*prevZ=first;
char*name=(char*)malloc(102*sizeof(char)),*name2=(char*)malloc(102*sizeof(char));
getchar();//获取调用z()时可以从main保留的“\n”
fgets(name,102,stdin);//扫描用户要删除的作者
而(名称[j]!='\0'){
如果(名称[j]='\n'){//从用户输入中删除行尾字符
名称[j]='\0';
打破
}
if(name[j]>='a'&&name[j]authors);//从当前列表项获取作者名称
while(name2[j]!='\0'){
if(name2[j]>='a'&&name2[j]next=nextZ->next;//从列表中删除条目
nextZ=nextZ->next;
}
else{//如果字符串不匹配,则列表移动到下一个条目
prevZ=prevZ->next;
nextZ=nextZ->next;
}
}
printf(“%d个已删除的条目。\n”,i);//打印出已删除条目的数量
}
1.要了解strstr()
的工作原理,请编写一个简单的main,它除了玩strstr()之外不做任何其他事情
.2.为了了解你的程序到底是如何使用调试器一步一步地跟踪它的,在每一步都要检查所有相关变量。顺便说一句:代码在这里引入了一个大内存泄漏:name2=nextZ->authors;
Idea:Change thisprintf(“%s”,name2);
变成printf(“name2='%s',name='%s'\n',name2,name)
嘿,谢谢,我解决了主要问题——我将行尾字符加载到name中,因此它找不到该字符串。但是,name2=nextZ->authors;中的大内存泄漏是什么意思呢?看看你在指针name2
时分配了什么。
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
}