C_67 C Program to concatenate two strings | with strcat() and without strcat()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the series of learning programming in c we are discussing some programs on strings in the previous video we have discussed how to find out a length of a string using predefined function that is strlen as well as without using predefined function we have written our own logic right so now in this video we will talk about how to concatenate two strings we will use for this concatenation uh the predefined function as well as we will write down our own logic without using that predefined function right first of all i'll tell you what does this concatenation means we'll properly take one example and we'll see everything first of all the logic i'll show you on the sideboard then i'll show you practically on my laptop the program both the programs using predefined function as well as without using predefined function right so now see first of all what does this concatenation means see i am taking two strings suppose i am taking s1 see these are two string i am taking one is jenny second is khatri and i have suppose declared s1 and s2 and size is what i am declaring this string something like this so size of s1 is 6 only and s2 is 7 only i am storing jenny and khatri so index would be from 0 to 5 from 0 to 6 size -1 this is s1 and suppose address is 1000 one thousand and two and thousand five and suppose address of this one is uh 1046 47 48 21 52 something like this we have and in s1 we have what address of this the first byte that is the base address of this one and in s2 we have base address of this one that is one zero four six right now what does this concatenation means i am going to store sorry i am going to append this s2 this string to this s1 like what output i want this kind of output jenny khatri this is what concatenation we are going to concatenate or combine two strings so we are going to append this one or you can say more precisely if i say we are going to append a copy of the string right obviously we are going to not going to remove k here and we are going to you know place k here obviously we are going to keep we are going to append a copy of this string at the end of this s1 right or you can also say you can also do something like this like khatri and jen that is also fine but ultimately it is what appending of either this string to this or this string to this at the end of the string right now first point you have to take care about concatenation is what if i want to concatenate this string here then obviously this string s1 string should be large enough but here we don't have the size size is only 7 so where you will store the string so the size of this s1 should be or you can say the destination string here i am taking s1 is what a destination string and this one is source right we are going to append this s2 to s1 so first of all you need to take care the size of s1 should be large enough to accommodate the string s2 string or the source string so better to take size of s1 suppose i am taking 30 or 50 you can take right okay first point is this one next is what c now how to use a string concatenation function predefined function is strc80 to concatenate two string strc80 right and it accept two arguments fine string one string two or you can say destination string and source string if see please uh you know watch this or listen to this point very carefully if you want to append this s2 string at the end of this s1 then at first we will write s1 and then s2 means here the first argument is always destination string and the second would always be source string so the s1 would be appended to s sorry s2 would be appended to s1 and it will return what a pointer to destination string because now finally our definition string is jenny cutry so it will return pointer to the destination string and we can directly print now s1 or you can set the destination string but if you want to append this one i want this output khatri so now i want to append s1 at the end of s2 so now how you will use string cad here s2 here s1 always first argument would be destination string at the end of which string you want to add the another string and this one would be source string now output of this would be any output or this would be jainikha three i hope you got you know the difference right now next thing see now it is already defined the definition of you can say it's already you know declared in which header file string don't edge because whenever we use this inner program obviously compiler doesn't know the meaning of string cat you know compiler you know we have to tell compiler the meaning of string card and the meaning is already you can say the meaning is already there in which header file string dot h that is why we include string to attach in a program so that compiler can refer the string dot h and know what exactly the meaning of string at and do you know whatever you want like concatenation of these two strings right so if you use this predefined function you have to do nothing it's very simple right first of all we'll write this program here but here you know one problem with with this string cad function is what buffer or overflow it is not going to check the size of destination string i am going to take this example this one right destination string is s1 suppose here buffer size is only 10 or you can say is 6 only so obviously we cannot append khatri here because we don't have a located size in s1 we don't have more size but string cat will still append or still can still concatenate this here khatri right where see this k would be here at the place of null character so here k h a t r i and after that this null that is what concatenation right but we don't have any space but still concatenation will concatenate with this thing so obviously it's not safe right that is one problem this with the string cat and i hope you got the meaning of string concatenation function and now i hope let me write down the program now let me write down a program include this string dot h i'm taking s1 suppose 30 that is jenny s 2 7 ka three and simply write string cat s1 comma s2 and simply print printf percentage s what you will print we are concatenating s2 we are appending a copy of s2 to s1 so we will print s1 simply because this is a destination string right s1 so here khatri would be something like this because it is of 30 size is 30 so we have till 29 so this null would be replaced with this k first character of the this source string k h a t r i and at last this null also obviously it's not 29 5 6 7 8 9 10 11 and further we have index still 29 because the size is 30 right so this is very simple if you are using this trinket function fine but let me write down the logic without using string cat function how you will do see first of all think how you can do this obviously we have to append this first character of s2 string or the source string to here the null character where we have null character of destination string right so we should know what is this index that is 5 or you can say we should know length of the string s1 string right as well as we should know length of s2 string why so because you are going to append you are going to append what k then h d r i and then this one so obviously till length we are going to till s2 length we are going to append the string to s1 string so you need to know length of this also and this also first of all find out the length of both the strings so here suppose i am taking length 1 and i am using the function strlen s1 and in length 2 strlen s2 and before using this you have to declare l e n 1 and l e n 2 or you can say unsigned in ln 1 ln2 now the value of this this variable ln1 and length 2 is length of s1 is 1 2 3 4 5 here we have null right so excluding null character is one two three four five so in l one we have five in s2 one two three four five six six we have right now see write down a loop i'm using for loop here because obviously we are going to append one by one first this then this then this like this right so i'm taking i is equal to 0 and i less than c now loop should run the upper bound condition should be what think we are going to append this to this so we are going to traverse this one till null right so upper bound should be and what is the length of this obviously we have found the length of this length is six so upper bound should be six till this null character we are going to append this i hope you got my point so upper bound should be here six so now l two is six so till l length 2 or you must write less than equal to because obviously we want sixth index also we also want null null sixth index also so it should be less than equal to length 2 and i plus plus from 0 to 6 that is why from 0 to 6 now whatever in s2 we are going to append to s1 but what should you what you will write here and here this index is going to be started from 0 only 0 1 2 3 4 5 6 like this so here simply write i i would be 0 1 2 3 4 5 6 right but what you will write here s 1 where you will append this k here at the place of null character means the index is 5 right and here length is also 5 so what we can say how you can take here index alien 1 maybe you will say just write lin 1 because index we will get 5 by using alien1 okay first is fine but next time we want index sixth index this i would be 1 h i want to store here here i want i want to store k so i would be 1 next time and here alien 1 is again 5. so it will append h also to here to this index fifth index so k would be replaced with h and that's we don't want we also want to move this index 5 then 6 then seven then eight then nine ten like this one by one we want to add one one one so better to what you can write here we cannot write simply and one you have to add i right why i'm adding this i let me just try it and then you will get it and that's it that is simple logic and after this for loop you can print printf percentage is s1 that's it see now ln one five length two is six okay now five is what zero zero less than length two length two we have uh length two is 6 yes condition true enter now s2i s2 0 0th index means s2 of 0 is k k would be appended to length 1 plus i length 1 is 5 5 plus 0 that is 5 only so s105 s105 is this so k would be appended here now i plus plus i becomes condition is still true now s two of one that is h would be appended now see length is five but now plus one that is s one of six and that's exactly we want h should be here at sixth index of s one so h would be appended here i plus plus i becomes two conditions still two enter now s two of two that is a a would be appended five plus i is two seven s 1 of 7 so a would be appended here that's exactly you want right this is what concatenation and till what i would be 3 then 4 then 5 and then 6. once i value becomes 6 yes condition still true length 2 is 6 again enter now s 2 of 6 s 2 of 6 is null character so till null it is going to append k t r i and null also it will append where length 5 plus now i value is 6 5 plus 6 11 at 11th index and that's what exactly we want right now i value becomes 7 but condition not true exit and print what s one so s one is now jenny khadri till it find null character it is going to print right so i hope you got this concept right and if you want to concatenate like i want to concatenate only k h a t right one two three four four characters right so here we have another function str ncat and it takes three argument first would be destination string then would be source string as well as what the number of characters you want to concatenate here i want to continue one two three four characters so just pass four right and it will continue concatenate only jenny then kh 80 that's it till 4 character it is going to concatenate right so this is also another variant of string concatenation function now let me show you this program on my laptop so now let me create one file for you and i am taking string cat name is shrink at dot c header file string dot h as well like right and then main function and here i'm taking one string i'm taking s1 and size is 30. i am going to initialize it here only jenny right and one more string second is s2 size i'm not going to take the size of the string and just initializing okay now just use string cat function and s1 comma s2 one is destination s2 is what source now just print string is or you can say string after concatenation is percentage s and simply print what s one right oh with potes i'm also you know i also want to print s2 fine so better to use here what slash so that you will get output next line so now let me run this and see what output you are getting it should be first janika three and then see first is concatenation s1 is now containing concatenation that is janika three and this is is what s2 only right so yeah it is working correctly now see if i take size of this only 5 or you can say 6 so see size is 6 only but it is going to append still see it is going to append jenny khatri it is not going to check the buffer of s1 so that is what buffer overflow problem right that is risky to use now let's uh you know comment this out and now we will use our own function so now i'm first of all finding i'm taking l1 equal to strlen and s1 i am finding the length of this and in l2 finding length of s2 right so before using this we have to declare l 1 and l 2 as well as i because we will use for loop now write down for loop i should be 0 i should be less than l 2 sorry less than equal to right because null also we want and i plus plus and within this just write down s 1 what l1 plus i equal to s2 of i right and printf after concatenation s1 i think it should run let me just run this yeah string after the concatenation is c j kha three and this is what khatri only s2 i'm going to print so this is what without using string head function right now let me just you know use that function str ncat see it is going to take what three arguments right so here till three character i want to concatenate right and see just comment this out see this is what now just print concatenation string and what it will print see jenny and only it is concatenating kha only three characters so this is what string str and c80 up to n character it will concatenate right so next video will see another program on string so now i'll see in the next video till then bye bye take care
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 18,126
Rating: 4.968689 out of 5
Keywords: data structure tutorials, operating system, data structure and algorithms, jayanti khatri lamba, jennys lectures, jenny data structures, jennys lectures DS, jenny lamba, jennys baby, jennys lectures baby, data structures, c programming tutorials for beginners, strings in c, program for concatenation of 2 strings, pointers in c, functions in c, gate cs lectures, ugc net computer science preparation material, c jennys lectures, dbms jennys lectures
Id: LFPoPHBiWCE
Channel Id: undefined
Length: 21min 29sec (1289 seconds)
Published: Mon Aug 02 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.