duplicar cadena
C
#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:
- Title
- disable gnu++11 option
- Category
- C
- Title
- c check if array is empty
- Category
- C
- Title
- changing tuple values
- Category
- C
- Title
- keras conv2d batchnorm
- Category
- C
- Title
- CL/cl.h: No such file or directory
- Category
- C
- Title
- read from stdin c
- Category
- C
- Title
- write array of char to file in c
- Category
- C
- Title
- battlefield4u.com
- Category
- C
- Title
- download android ndk r 16
- Category
- C
- Title
- c defined value sum
- Category
- C
- Title
- wait function in c
- Category
- C
- Title
- functions return type in c
- Category
- C
- Title
- write a binary file c
- Category
- C
- Title
- how to comment in arduino
- Category
- C
- Title
- c program to find the sum of given number
- Category
- C
- Title
- uninstall elgg from hostgtor
- Category
- C
- Title
- c vs python
- Category
- C
- Title
- c list
- Category
- C
- Title
- Fibonacci program c pthread
- Category
- C
- Title
- ModuleNotFoundError: No module named 'tensorboardX'
- Category
- C
- Title
- can we write a program without main in c
- Category
- C
- Title
- c bit access union
- Category
- C
- Title
- change plot line color in matplotlib
- Category
- C
- Title
- code wars responsable drinker
- Category
- C
- Title
- variadic function in c
- Category
- C
- Title
- v
- Category
- C
- Title
- c program to find the sum of given number using recursion
- Category
- C
- Title
- buscar caracter
- Category
- C
- Title
- c check if file was created
- Category
- C
- Title
- how to check the size of a file in linux c
- Category
- C
- Title
- convert string to float c
- Category
- C
- Title
- arduino knn
- Category
- C
- Title
- c printf uint32_t
- Category
- C
- Title
- function pointer c
- Category
- C
- Title
- potencia recursiva
- Category
- C
- Title
- what is strikethrough in markdown
- Category
- C
- Title
- last element from list javascript
- Category
- C
- Title
- boolean function c
- Category
- C
- Title
- prime number
- Category
- C
- Title
- primo
- Category
- C
- Title
- java.lang.SecurityException: Permission denied (missing INTERNET permission?)
- Category
- C
- Title
- es vocal
- Category
- C
- Title
- slug urls django
- Category
- C
- Title
- bash: apt-add-repository: command not found
- Category
- C
- Title
- atoi c
- Category
- C
- Title
- arduino digital input pins
- Category
- C
- Title
- does strcat null terminate
- Category
- C
- Title
- text wrap terminal colour
- Category
- C
- Title
- strtok
- Category
- C
- Title
- limit of integer in c
- Category
- C
- Title
- how to rebasde
- Category
- C
- Title
- entete c/c++
- Category
- C
- Title
- check if string in string c
- Category
- C
- Title
- where is /dev/kvm
- Category
- C
- Title
- c program to count duplicates in an array
- Category
- C
- Title
- Declare macro
- Category
- C
- Title
- sum of arrays
- Category
- C
- Title
- sbatch array set max jobs at once
- Category
- C
- Title
- powershell list big files
- Category
- C
- Title
- docker logs follow
- Category
- C
- Title
- printf n characters c
- Category
- C
- Title
- vbl share price
- Category
- C
- Title
- update ubuntu in terminal
- Category
- C
- Title
- how to get random numbers in c
- Category
- C
- Title
- '&&' within '||'
- Category
- C
- Title
- how to use ? in c
- Category
- C
- Title
- restapi
- Category
- C
- Title
- latex font sizes
- Category
- C
- Title
- bella ciao lyrics
- Category
- C
- Title
- download file by command line windows
- Category
- C
- Title
- oop244
- Category
- C
- Title
- how to download file in powershell
- Category
- C
- Title
- classification report to excel
- Category
- C
- Title
- where is my vimrc
- Category
- C
- Title
- how to go to top of file in vim
- Category
- C
- Title
- set timezone in debian terminal
- Category
- C
- Title
- how to mutex lock in c
- Category
- C
- Title
- sdl bouton
- Category
- C
- Title
- how to create an array of char in c++
- Category
- C
- Title
- cantidad de digitos recursiva
- Category
- C
- Title
- print in c
- Category
- C
- Title
- functions in c
- Category
- C
- Title
- what is x:Name Xamarin forms
- Category
- C
- Title
- múltiplos entre dos numeros en c
- Category
- C
- Title
- mediawiki upload size
- Category
- C
- Title
- version of libgcc
- Category
- C
- Title
- c copy string
- Category
- C
- Title
- how to globally initlailize a struct
- Category
- C
- Title
- arduino millis()
- Category
- C
- Title
- use of matplotlib inline
- Category
- C
- Title
- c value set to zero __memmove_avx_unaligned_erms
- Category
- C
- Title
- printf("%3d ",XX);
- Category
- C
- Title
- how to declare 2 d array using malloc
- Category
- C
- Title
- compare two chars c
- Category
- C
- Title
- entity framework core discard changes
- Category
- C
- Title
- how to pass an array value to a pthread in c
- Category
- C
- Title
- what is stdin in c
- Category
- C
- Title
- isalpha c
- Category
- C
- Title
- docker images command
- Category
- C
- Title
- C why is is & nit used in scan f fr string
- Category
- C