当我运行这个程序时,我引入了例如stringAna are mere
和substringare
。结果字符串是Ana mereere
,而不是Ana mere
。你能告诉我为什么吗?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
char *str_rem(char *sir, char *subsir)
{
int i, j,k;
int loc = -1; //subsirul nu a fost gasit(presupunem)
for (i = 0; sir[i] && (loc==-1);i++)
for (j = i, k = 0; sir[j] == subsir[k];j++,k++)
if (subsir[k + 1] == 0) loc = i;
if (loc != -1) //subsirul a fost gasit
{
for (k = 0; subsir[k]; k++)
; //bucla nu face nimic!! dorim sa obtinem cate elemente are subsirul
printf("\nSubsirul este format din %d elemente\n", k);
for (j = loc, i = loc + k; sir[i]; j++, i++)
sir[j] = sir[i];
sir[i] = '\0';
}
return (sir);
}
void main()
{
char *sir;
sir = (char*)malloc(255*sizeof(char));
printf("Introduceti sirul: ");
gets(sir);
char *subsir;
subsir = (char*)malloc(255 * sizeof(char));
printf("Introduceti subsirul care doriti sa fie cautat: ");
gets(subsir);
printf("Sirul %s rezultat dupa stergerea subsirului %s este: %s\n", sir, subsir, str_rem(sir, subsir));
_getch();
}
最佳答案
生产线
sir[i] = '\0'
应该是
sir[j] = '\0'