duplicar cadena
#include <stdio.h>
#include <stdlib.h>
// i i i i i
// 0 1 2 3 4
// c a s a 0 (cad)
// - - - - - (nueva)
// devuelve una copia dinamica de la cadena pasada como parametro.
char* strdup(char cad[]){
char *dupli;
int i = 0;
while(cad[i] != '\0'){
i++;
}
dupli = (char *)malloc((i + 1) * sizeof(char)); //ya tengo la cadena con su tamaño adecuado
i = 0;
while(cad[i] != '\0'){
dupli[i] = cad[i];
i++;
}
dupli[i] = '\0'; //el \0 no entra al bucle, lo tengo que copiar yo.
return dupli;
}
int main(){
char cadena[100];
printf("Introduce una cadena: ");
scanf("%[^\n]%*c", cadena);
char *duplicada = strdup(cadena);
printf("La cadena es => %s\n", duplicada);
free(duplicada);
return 0;
}
Also in C:
- random en c
- even odd c
- linux kernel write()
- arduino ip to string
- how to create random integers from a specific range in c language
- get the latest field in mongodb collection
- change text color c
- angle between two points
- is 33 prime number
- what is string::npos
- lognormal distribution - matlab
- find factoril in C using recursion
- how to open chrome using cmd
- strdup c
- how to make infinite loop in c
- 1 212 32123 4321234 543212345 in c
- download file by command line windows
- counter program in c language
- uint32_t extract first bits
- find the largest number in else if javascript
- how can i remove a specific item from an array
- what is constructor
- arduino vscode upload choosing sketch
- es palindromo