Aquí te dejo una solución, hecha rápida, puede hacerse mejor, ese trabajo te lo dejo a ti. Como el caso de no requerir espacios y que ruede los caracteres la misma función.
Código C:
Ver original#include <stdio.h>
#include <string.h>
char *
p_strcat(dest, src, position, len)
char *dest;
const char *src;
unsigned int position;
size_t len;
{
/* sanity check */
if (position < 0 || len <= 0 || position > len ||
return NULL;
unsigned int i = 0;
unsigned int pos = 0;
char * aux = dest;
for (; *aux != '\0'; aux++, pos++)
if (pos == position) break;
for (; src[i] != '\0' ; i++)
{
*aux = src[i];
aux++;
}
return dest;
}
int
main (int argc, char *argv[])
{
char aux1[60] = "Mi nombre es: de Venezuela.";
char aux2[8] = "Alberto";
p_strcat(aux1, aux2, 14, sizeof(aux1));
return 0;
}
En la solución de efcisa, tiene unos errores y observaciones.
Código C:
Ver originalchar s1[100] = "Una vez.", s2[] = " sola opcion a la";
En la función, vas a pasar al final toda la cadena a
s1 pero a esta la definiste solo el espacio de
"Una vez." malloc devuelve un puntero nulo, por lo cual no es necesario tener el
case, si es por legibilidad es mejor
Tiene otros errores, que seguro son con las funciones strcat y strcpy, a lo mejor porque no está especificado el caracteres final
Código test:
Ver original==8453==
==8453== HEAP SUMMARY:
==8453== in use at exit: 0 bytes in 0 blocks
==8453== total heap usage: 1 allocs, 1 frees, 25 bytes allocated
==8453==
==8453== All heap blocks were freed -- no leaks are possible
==8453==
==8453== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 8)
==8453==
==8453== 1 errors in context 1 of 2:
==8453== Invalid read of size 1
==8453== at 0x40086AD: strcpy (mc_replace_strmem.c:442)
==8453== by 0x8048614: insert (in /home/guzzano/test)
==8453== by 0x8048560: main (in /home/guzzano/test)
==8453== Address 0x4028041 is 0 bytes after a block of size 25 alloc'd
==8453== at 0x4007282: malloc (vg_replace_malloc.c:270)
==8453== by 0x80485AD: insert (in /home/guzzano/test)
==8453== by 0x8048560: main (in /home/guzzano/test)
==8453==
==8453==
==8453== 1 errors in context 2 of 2:
==8453== Invalid write of size 1
==8453== at 0x4008321: strcat (mc_replace_strmem.c:267)
==8453== by 0x8048602: insert (in /home/guzzano/test)
==8453== by 0x8048560: main (in /home/guzzano/test)
==8453== Address 0x4028041 is 0 bytes after a block of size 25 alloc'd
==8453== at 0x4007282: malloc (vg_replace_malloc.c:270)
==8453== by 0x80485AD: insert (in /home/guzzano/test)
==8453== by 0x8048560: main (in /home/guzzano/test)
==8453==
--8453--
--8453-- used_suppression: 12 U1004-ARM-_dl_relocate_object
==8453==
==8453== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 12 from 8)
Saludos