Create Your Own strcpy() String Copy Function | C Programming Example

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Stumbled upon this video and started watching it, I really didn't expect to learn what I did. Thanks for uploading this.

octophrator
Автор

THANK YOU SOOO MUCH!!! IVE BEEN STUCK WITH THIS FOR LAST 3 DAYS AND YOU FINALLY SORTED IT.

rahqanshaikh
Автор

Thanks for the great explanation.

@2:25, to print out the pointer values for dest_ptr, I was wondering why you have it as

printf("dest_ptr: %p\n", dest);

and not

printf("dest_ptr: %p\n", dest_ptr);

ulquiorra
Автор

if the goal is to make your own function why do you include the string header file, I’m a beginnerr and this is new to me so i just need little hint.

issabello.
Автор

Thank you a lot, this is really was helpful.
Please keep going😊

SilentxMod
Автор

Please make a video on creating the strncpy function in c.

ransfordwelbeck
Автор

that was awesome. Thank you so much :D

heberalturria
Автор

Question... Is this useful? How can this be applied in a practical situation?

josiahedwards
Автор

Wow perfect! I could understand it very well, but I have a doubt if I want that the arguments come from the command line, how would I do it?

lorenaquijadaleon
Автор

So What if destination array has less size than source array? It doesn't work, I mean it just does not check!

nauadev
Автор

Hello Sir I want to build my own openCv library kindly make a tutorial on it also kindly help me in making own functions like printf and scanf in java

anujg
Автор

I am actually dealing with the same thing right now. But I want to ask something, what if source is bigger than destination? Shouldn't you specify a condition for it? So the program won't take this condition into account. As far as I know that strcpy will give us an error if the source is bigger than the destination. 

I came up with the code below: (Can you look into it?)


char *str_cpy(char *dest, char *src)
{
int i;
int counter;

i = 0;
counter = 0;
while (src[i] != '\0')
{
if (dest[i] == '\0') // if it encounters with null it is going to change counter as 1 and won't do the copying.
counter = 1;
i++;
}
i = 0;
while (dest[i] != '\0' && counter == 0)
{
dest[i] = src[i];
i++;
}
dest[i] = '\0';
return (dest);
}

MrBolcetik
Автор

Thank you DR kevin :

char* strcpy(char* dest, const char* src) {
int n {} ; // counter counting the number of elements in src
while(src[n] != '\0') { // we assume here that src is a null terminated string
++n ; // here we are using n as counter and also as index i never done this before to be honest
}
// we can now put the null terminator of dest at index n the same as src
dest [n] = '\0' ;
// now we loop using n as control loop
for(int i {n-1} ; i>=0 ;i--){ // i'm using decrementing loop just for showing off
dest[i] = src[i] ;
}
// we return dest
return dest ;
}
char* strcpy2(char* dest, const char* src){
char* podest = dest ; // because we want to return this address which is the beginning of the object that dest is pointing to
if(dest != nullptr && src !=nullptr){
while (*src != '\0')
*dest++= *src++ ;
}
// we add the null terminator
*dest = *src ;
return podest ;
// hopefully i didnot make any mistake here thanks DR kevin well explained as always
}

justcurious