Funkce strReplace() v jazyce C

Funkce strReplace() je funkce, která nahrazuje řetězec v řetězci.

Princip funkčnosti spočívá ve spočítání velikosti nového (výsledného) řetězce, alokace potřebné paměti a samotné nahrazovaní.


Pro své účely si ještě napíšeme jednoduchou funkci strpos(), která vrací pozici podřetězce v řetězci.

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

int strpos(const char *find, const char *string, int start = 0)
{
	int slen = strlen(string),
		flen = strlen(find);

	if (start >= slen)
		return -1;
	for(int i = start, j = 0; i < slen; i++)
	{
		for(j = 0; j < flen; j++)
			if (string[i+j] != find[j])
				break;
		if (j == flen)
			return i;
	}
	return -1;
}

char *strReplace(const char *find, const char *replace, const char *str)
{
	int f_len = strlen(find),
		r_len = strlen(replace),
		s_len = strlen(str);

	int fcount = 0, fpos = 0;

	// zjistime kolikrat je hledany retezec find v retezci str
	while((fpos = strpos(find, str, fpos+1)) != -1)
		fcount++;

	if (fcount == 0)
		return (char *)str;
	// spocitame pocet potrebnych znaku pro vysledny retezec
	int str_size = (s_len - (fcount*f_len)) + (fcount*r_len);

	// alokuje pamet pro novy retezec
	char *str2 = (char *)malloc(sizeof(char)*str_size+1);

	int lastpos = 0, j = 0;
	for(int i = 0; i < s_len; i++)
	{
		if (lastpos <= i && lastpos != -1)
			lastpos = strpos(find, str, i);

		if (i == lastpos)
		{
			for(int k = 0; k < r_len; k++)
				str2[j++] = replace[k];
			i += f_len-1;
		}
		else
			str2[j++] = str[i];
	}
	str2[j] = '\0';
	return str2;
}

int main()
{
	char str[] = "Hello my big world";
	char *str2 = strReplace("big", "small", str);
	printf("%sn", str2); // vypise: "Hello my small world"
	free(str2);
	return 0;
}

79 Responses to Funkce strReplace() v jazyce C

  1. Pingback: LUKE

  2. Pingback: EDUARDO

  3. Pingback: BRANDON

  4. Pingback: PERRY

  5. Pingback: SALVADOR

  6. Pingback: ROY

  7. Pingback: ALFRED

  8. Pingback: JEFFREY

  9. Pingback: KIRK

  10. Pingback: RUSSELL

  11. Pingback: TERRENCE

  12. Pingback: EDUARDO

  13. Pingback: DONNIE

  14. Pingback: ROLAND

  15. Pingback: LEWIS

  16. Pingback: RONALD

  17. Pingback: DUSTIN

  18. Pingback: ALFRED

  19. Pingback: LESLIE

  20. Pingback: CLAUDE

  21. Pingback: BRETT

  22. Pingback: WILLIE

  23. Pingback: CHARLIE

  24. Pingback: DONALD

  25. Pingback: FELIX

  26. Pingback: WALLACE

  27. Pingback: VICTOR

  28. Pingback: BRADLEY

  29. Pingback: TONY

  30. Pingback: BRUCE

  31. Pingback: KENNY

  32. Pingback: WILLIAM

  33. Pingback: BOB

  34. Pingback: BILLY

  35. Pingback: JIMMY

  36. Pingback: BYRON

  37. Pingback: CARL

  38. Pingback: RUBEN

  39. Pingback: ADAM

  40. Pingback: SERGIO

  41. Pingback: CLIFTON

  42. Pingback: VICTOR

  43. Pingback: DARYL

  44. Pingback: LAWRENCE

  45. Pingback: LUIS

  46. Pingback: JEREMIAH

  47. Pingback: RODNEY

  48. Pingback: LLOYD

  49. Pingback: RAY

  50. Pingback: MARCUS

Napsat komentář

Vaše emailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *

*

Můžete používat následující HTML značky a atributy: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free