C String Library and String Copy Function - strcpy()

preview_player
Показать описание
C Programming: C String Library and strcpy() Function in C Programming.
Topics discussed:
1) Introduction to C string library.
2) String Copy function (strcpy).
3) The prototype of strcpy() function.
4) Example use of strcpy() function.
5) The prototype of strncpy() function.
6) Example use of strncpy() function.

Music:
Axol x Alex Skrindo - You [NCS Release]

#CProgrammingByNeso #CProgramming #Strings #StringLibrary #strcpy #StringsInC
Рекомендации по теме
Комментарии
Автор

This is the correct solution for strncpy

#include <stdio.h>
#include<string.h>
int main()
{
char str1[7]="Hello";
char str2[4];
strncpy(str2, str1, sizeof(str2)-1);
printf("%s", str2);
return 0;
}

Output: Hel

lokeshsenthilkumar
Автор

Why str2[sizeof(str2)-1] = '\0' works.
Recall when we say a[i]. i is an index and last index is given by n-1 where n is array lenth.(index start from zero).
In this example, 'H' is str2[0], the last char 'o' is str2[4] and NULL character will occupy index [5].
Str2[sizeof(str2) -1] = '\0';
Is identical to;
Str2[6-1] = '\0'; // assuming size of single char is 1 byte, length of str2 will be the same as array size.
Str2[5] = '\0';

lawrencemuthui
Автор

For str1[6] and str2[4]
We have to add str2[4]='\0' after the strncpy line.Otherwise it will print HellHello. (5:25)

#include<stdio.h>
#include<string.h>
int main()
{
char str1[6]="Hello";
char str2[4];
strncpy(str2, str1, sizeof(str2));
str2[4]='\0';
printf("%s", str2);
return 0;
}

ranjanmb
Автор

The correct code to print "Hell" for str2 is (5:25) :


#include <stdio.h>
#include <string.h>

int main(){
char str1[6] = "Hello";
char str2[5];
strncpy(str2, str1, sizeof(str2)-1);
printf("%s", str2);
return 0;
}
Please check if I am correct.
(the problem was about the string terminator of str2 as I mentioned in my previous comment).

devanshuyadav
Автор

In the case of strncpy : (5:25)
If str2 is "Hell". As str2 is of size 4. then what about the string terminator?
shouldn't it be "Hel" only?

devanshuyadav
Автор

Great videos sir!
One kind request please complete this course as fast as possible.

dhruvkharkwal
Автор

You have said "if the length of string pointed by str2 is greater than length of the character array str1 then it will be undefined behaviour" but in the example you have shown str1[6] and str2[4] and i•e; size of str1 is greater than str2.

optimistichomosapien
Автор

the main question is from where did u learn c language? bcoz ur concepts are so clear

yagneshacharya
Автор

Thanks! It should be clear that strcpy should not be used. But always strncpy. Same applies for sprintf vs snprintf .. etc.

ArjanvanVught
Автор

sizeof works only on static defined arrays. Otherwise, strlen should be used.

ArjanvanVught
Автор

Fantastic vedio sir it is really helpful to understand full concept we are really thankful to you sir 👍🙏🙏🙏

govindsolanki
Автор

Really sir this presentation is great awesome and every all lovely.. I understand too much about strcpy

kunalsoni
Автор

Beginners need to understand K & R strcpy function.
No string header file needed.
strcpy (char * s, char * t)
{
while(*s++ = *t++) ;
}

jasonmudgarde
Автор

there’s a problem with this code. The strncpy function does not automatically append a null terminator ('\0') to the destination string if the source string is longer than the specified number of characters to copy. In this case, since str1 is longer than 4 characters, strncpy will not append a null terminator to str2.

This means that when you try to print str2 using the printf function, it will keep reading and printing characters until it encounters a null terminator somewhere in memory. This can cause unpredictable behavior and may even crash your program.

To fix this issue, you need to manually append a null terminator to str2 after calling strncpy. Here’s a corrected version of the code:

#include <stdio.h>
#include <string.h>

int main() {
char str1[6] = "Hello";
char str2[4];
strncpy(str2, str1, sizeof(str2));
str2[sizeof(str2) - 1] = '\0';
printf("%s", str2);
return 0;
}
Copy
This code will produce the following output:

Hell

padhlegadhe
Автор

str2[sizeof(str2) - 1] = '\0' ; we should use sizeof(str2)/sizeof(str[0]). We can't say that char is 1 byte for all machines.

prodiptamondal
Автор

sir at the last as you are saying for adding a null character u have said we have to write a code str2[sizeof(str2)-1] but sir in this it will be adding the null character to str[5] as in str[5] 'o' is stored so where o will be going

anishkumargiri
Автор

Strncpy (dest, src, sizeof(src)) will be a better option instead of sizeof(dest)

kausachan
Автор

thank you so much bro i like your channel it helps me alot and especially this video i was lost before that one

souhaillepacifique
Автор

The prototype is having arguments as char pointers. But, the problem explanation has char arrays. Why because we cannot change or modify char pointers? But, we can copy, right?

venkateshpolisetty
Автор

strcpy increases the size of the destination string by itself whereas strncpy gives undefined behavior

AlokSingh-jwfr