J'essaye de faire une fonction "replace", pour remplacer du text par un autre text dans une chaine de caractères.

Voici ma fonction :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
char * STR_REPLACE (const char *base, const char *find, const char *replace)
{
	char * pch;

	int lenght_find;
	int lenght_base;
	int lenght_replace;
	int lenght_newString;
	
	int start;
	
	char *newString=NULL;
	char *strTemp=NULL;
	
	int lenght;
  
	lenght_find = (int)strlen(find);
	lenght_base = (int)strlen(base);
	lenght_replace = (int)strlen(replace);
	newString 	= (char *) malloc(lenght_base);
	
	strcpy(newString,base);
  
  // tant que l'on trouve l'occurence
	while (pch = strstr( newString , find ))
	{
		size_t size 			= strlen (newString);
		lenght_newString 	= (int)strlen(newString) - lenght_find + lenght_replace;
		
		// réalloue une nouvelle taille à strTemp
		strTemp 					= (char *) malloc(lenght_newString);
		
		// recherche position du début de remplacement
		start = strlen(newString) - strlen(pch);
		
		memcpy (strTemp, strlen, start);
		memcpy (&strTemp[start], newString, strlen (newString));
		memcpy (&strTemp[start + strlen (newString)], &newString[start + (int)strlen(find)], size - (int)strlen(find) - start + 1);
		
		newString = (char *) malloc(lenght_newString);
		strcpy(newString,strTemp);
		pch = strstr( newString , find );
	}


  return newString;
  
}
Malheureusement, j'ai un Bus Error (core dumped) au niveau de mon while.

Une idée ?