C_97 Passing String as an Argument to a Function | C Language Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the series of learning c programming we are discussing functions in c in the previous video we have discussed how to pass an array as an argument to a function in this video we will discuss how to pass a string as an argument to a function or you can have to pass a character array as an argument like what is the string like if i want to pass my name it's nothing but but a string or you can say character array as an argument right so it's character array it means all the rules should be same as we we have discussed how to pass array as an argument rules should be same right now how to pass that will discuss here and then you know with the proper program and then i'll show you practical also that program on my laptop and we can also pass array of strings that is a different concept so that we'll discuss in a next video right but before that just want to tell you one thing if you want to build a great career in software development then you must think about the aspects like what's trending in the industry the questions being asked in the interviews how to improve your courtship ranking what is the thought process behind the great application like zometo ola so here an academy brings your platform where you can get access to weekly shows which you can watch live and the host of these shows are working in some of the top companies like linkedin google amazon and our seven star coders on code chef and industry experts with years of experience so they'll be covering the content which would be really helpful for your career like in the live episodes you will get an opportunity to ask take a about the top 20 questions being asked and the industry leaders about the recruitment process in top startups and mncs what is the eligibility criteria and how to apply for them and you can also get your resume reviewed by the experts not only this you can also participate into mock interviews and you can also learn courses on programming languages uh blockchain and crypto cloud computing tech aspects of digital marketing data analytics and there's one more show in which the host will walk through the top interview questions usually asked for the companies like apple netflix google amazon linkedin and how to solve them and respond in a way that you know helps you in cracking the interview and you can also get a chance to come live with the host and you know get your query resolved on the spot so if you are a college student and you know want to get placed in the top companies or if you are a working professional and want to switch then this show is definitely for you so the detail of the show and the batch you will find in the link that i'll put in the description box of this video and don't forget to use my code jkl10 to get 10 extra discount on your paid subscription so now let's discuss how to pass a string as an argument to a function so rules would be same that we have discussed when we are passing array as an argument because string is nothing but but a character array so rules should be same right so now we'll take a simple example first just to display the string we'll pass a string and that will display the string in that function only right we'll call that function using you know a passing string as an argument so i am taking one function display see we are going to just pass the string we are you know i am not supposed that the function should return something so the return type is void and suppose name is display and what we will pass a character array so just write down in the bracket this thing data type and this is what like it's an array or you can write down this also cat s freak this also means that it would be a generally we you know declare string something like this this also right or you can write something like this fine now suppose in main function what i'm going to do i'm taking a string cat str and here only i am taking jenny or you can ask from the user also it's up to you like printf interesting and scanf percentage s and address of you know this str but i'm initializing at compile time only and i want to call this function display and just pass how you will pass just write down the name of the string str that's it now how to define this function void name display and here obviously we have to take a string or you can say character array to accept this as an argument right but see either i am taking care str name can be same or different str no need to specify the size here right now here i want to just i want to display like printf and string is percentage s and the string is what str that's it right so here what does this mean it's not like that the complete string jenny would be copied here one copy would be here one copy would be would be here no it is what call by reference string would also be passed as call by reference so what str means star contains the base address of the character array or you can say the address of the first character so that address would be passed so this will act as what compiler will interpret as can asterisk str means str is a pointer to this string so ultimately it is called by reference it's not a duplicate copy it's not a copy of this string right so now i guess no need to explain this or whenever the control you know when you execute this control will go here now we are taking one string so what in main one in step one frame would be for this main function right and the activation record of this main function would be there so here we are having what this str as a pointer and here i am taking what j e double n y jenny so this str would point to here suppose address is 100 1 0 1 1 0 2 1 0 3 1 0 4 because character will take one byte only so here we have 100 in this now in display str we are going to pass str so means it is going to pass the address so here in display whenever it will be called here second frame would be for display function here we have one pointer that is str and here at 100 would be copied so now this str would also point to this j right or you can take str1 any different name and just print the string percentages str i hope you know how to you know this process how to you know print a string that we have discussed when we were discussing another the concept strings in c so you can watch out that video in the c playlist the link of that place i'll put in the description below this video so now jenny would be printed right and if you want to modify something in this journey here or suppose i just i'll modify this program we are going to find out the length of the string we will pass the string and we will find out the length of the string right or suppose you want to pass two strings lets take two string character one and one is character 1 and character this string is 2 so here i am passing what jenny and here i am passing khatri 2 string you want to pass right so str1 comma str 2 or the function name i'm taking modify right over here also i'm taking modify or i want a program like for one string i want to find out length of the string but for this string i want to modify the string right means suppose rather than this k i want here it should be it's some small k so i want to replace it with capital k just this k only or you can replace it with any other letter also like a or any other letter so here in the this declaration also means we have to specify like we want to pass two string so here also for accepting we have string one and here also we will have care str 2 and this name you can take different also right and what i want to do here i want to find out length of the the string first string so how to find out length of the string see one for loop i would be zero till till str of i not equal to null character and i plus plus and here i am taking one variable length is equal to length plus one and simply i just want to print printf length of first string is length would be in integer percentage d and l n g th length so we are taking two variable here i and length so we have to declare these variables in i and length in this function length is equal to zero i will initialize this here and for second string i want to modify so str2 and suppose i want to modify what this character second character so second would be at index one index would be started from zero so at one what character i want to replace this with what again k so just write down k either small or capital it's up to you right and here simply print printf you can print both the string also percentage s percentage s str1 comma str 2. that's it so how how it will be executed first control will go here in main now we have str1 and str2 so one string is this and one string i am taking this one right and suppose address is 200 201 202 203 204 205 so for this in str2 we have 200 so now this str2 would be pointing to where from here i am going to point this here it is pointing and this str1 is pointing to here str1 str2 you are passing means 100 200 would be passed the address so address will be stored in this str1 here also we have str2 so it is also 200 so this is also pointing to here now how to find out length i is equal to 0 first of all one more variable we have here i and one is length in length we have 0 i 0 stri not equal to null character see we know we know how the string would be stored at last it will be having null character automatically compiler will place a null character after the string just to show that it is end of string automatically compiler will do this thing right you no need to specify something like this null if you initialize know in a different way then you have to put null there right see stri means str 0 str of 0 means at 0 index what is the value i mean it should be str1 because i am i want to find out length of the first string str one this string so str one i i zero zero one two three four five it is j it is not null so condition true enter here length is equal to length plus one now length becomes one i plus plus i becomes two and again half length would be calculated this we have already discussed this program so no need to know briar in this full loop i hope you know the working of this for loop so now length would be what one two three four five so length would be five in length now at last we will have five so just print it will print five and str2 of one now now str2 str2 this is the pointer this is pointing to here now and at one index here 0 1 2 3 4 5 6 we have h so at the place of h just place k so what it will print str1 would be jenny str2 would be what k k a t r i this so we are modifying the string in this function right but once control will go here then control will go back to here right so the changes would be reflected here also and after after this what is the statement just closing braces so that's it exit from the program right so this is how you can it's very simple i guess if you go how to pass array as an argument you can also pass a string as an argument the tricky one is how to return a string from a function that is a different thing here we are not returning but if you want to return a string i want this function should return a string so how that thing would happen that we will discuss in next video that is a little bit tricky one right so let me just run this on my laptop and show you what output you will get same like here we are calculating length of the string you can also do like if you just convert the string from uppercase to lowercase or lowercase to uppercase many type of programmer string copy program string concatenation program whatever you can do with the predefined function of the string that you can do also with user defined function means you can define define your own function something like this and you can do all that thing just write down your own logic and you can do that thing also so you can try out different different programs also on string so now let's run this code so now let me create a new file here string as argument right and what program we will see just to find out length two string we are going to pass right so i am taking what just name i'm taking modify so here two string i want to pass so here in declaration just write down this thing and in main thing function what will take care str1 is equal to jenny and one more string i want to take care str2 and that i'm initializing at compile time only right so just call the function modify and just pass what str1 name of the string and str2 that's it right and now see definition of this one so here also we'll take what str1 or you can take different name also it's up to you and str2 i'm taking same name right now what you will do here two variable in i and one is i am taking length l right just find out length of the string one so what you will do str1 or you can find out length of the string tool so here you can write down str2 and i not equal to null it's up to you not equal to what null right and i plus plus so now here simply l is equal to l plus 1 or l plus plus or l plus equal to 1 there are many ways to write down this statement no need to i'm not writing the curly braces because the in the you know if there's four block because here we are having only one statement so no need to put the statement in the braces sorry or if you want to put that's a good practice always i think you should put if you know there is only one line that also you have to put that into bracket suppose later if you want to add any no more line in this for loop so at that time there will be no problem right now after that uh what i'm i want to do str2 str2 of 1 is equal to and here k only or any other characters you can say z want to place that with z and just print length of string 1 is percentage d and l because that could be that would be a integer value and both those strings are percentage s and percentage s just print str1 comma str2 that's it right here just put a slash fine now see let me run this and see what output you are getting see length of the string one is five obviously length is five jenny five excluding null character and both the strings are one is jenny and second one is c rather than h it is printing z k z a t r i right and see here i have modified this thing rather than this i am taking care astrick str1 care s3 str2 obviously that would be a pointer interpret compiler will interpret it as a pointer so rather than writing hair bracket you just you can write down this thing also and in modifying the declaration also you can just write down characteristic characteristic there will be no problem because here pointer which is pointing to a character type this this means what it would be a pointer which is pointing to a character so same thing now see let me just run this and see same output you are getting length of the string is five and both the strings are journey and the modified string so here you can also write down this thing also right but here if you declare the string something like this here see one more way of declaring string is care and s trick rather than this because what you think str1 str2 are pointers right so here also we can write down this thing str1 is a pointer which is pointing to a character status is also a pointer which is pointing to a character right and jenny and kathry will just pass these character i mean these values address so this would also be same right so now let me run this and show you what output you will get here in this case see it will not run has stopped working now why this happened this is for your assignment you have to tell me in comment box or if you are not getting the answer you can ask me in comment box i'll explain it in the later videos right here you cannot put something like this here you have to write down this declaration this one's all right and now let me run this and see it will give you output the correct output right so that's it for this video now so in the next video we'll see how to return a string right so until the next video till then bye take care you
Info
Channel: Jenny's lectures CS/IT NET&JRF
Views: 7,136
Rating: 4.948936 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, how to pass string as an argument to a function in c, passing array as an argument to a function in C, c language tutorials for beginners, what is function in c, what is array in c, c jennys lectures, gate cs lectures
Id: gW2xHKk1cBA
Channel Id: undefined
Length: 23min 43sec (1423 seconds)
Published: Mon Sep 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.