Ver Mensaje Individual
  #7 (permalink)  
Antiguo 18/09/2013, 21:52
ecfisa
 
Fecha de Ingreso: julio-2012
Mensajes: 133
Antigüedad: 11 años, 10 meses
Puntos: 22
Respuesta: duda con sub cadenas en programacion C

Cita:
Iniciado por eke_ps Ver Mensaje
no hay forma entonceS?
Hola eke_ps.

Código C:
Ver original
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void insert(char *, char *, int);
  6.  
  7. int main()
  8. {
  9.   // cadenas de ejemplo
  10.   char s1[] = "Una vez.", s2[] = " sola opcion a la ";
  11.  
  12.   insert(s1, s2, 3);
  13.   printf("%s\n", s1);
  14.  
  15.   return 0;
  16. }
  17.  
  18. void insert(char *s1, char *s2, int pos)
  19. {
  20.   int length = strlen(s1) + strlen(s2);
  21.   char *s3 = (char*) malloc(length);
  22.  
  23.   strncpy(s3, s1, pos);
  24.   s3[pos] = '\0';
  25.   strcat(s3, s2);
  26.   strcat(s3, s1 + pos);
  27.   strcpy(s1,s3);
  28.  
  29.   free(s3);
  30. }

Saludos.