C_65 C Program to Read and Print a String | C Programming Tutorials

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in the series of learning programming in c we have discussed theory about strings like what is string how to read a string scanf how to use scan a function gate is function printf and what is function right in this video we will see a simple program like how to read a string and how to print a string we will see practically not on this whiteboard because we have discussed the syntax of you know how to read and string how to print testing that we have discussed in previous videos so in this video i'll show you the program on my laptop right we will use both scanf and get this function printf and put this function and i'll show you everything like how you can use those functions in different different ways right but before that just want to tell you one thing for those who want to become software developer an academy brings you a platform where you can get access to weekly shows which you can watch live some of the shows that are going live soon on academy are hiring updates in startups and major tech companies live moc interview sessions pre-placement talks where hrs from the top companies come and talk about their hiring process top 20 questions which are asked in interviews and how to solve them weekend projects which would make you resume stand out in the crowd live journey of fellow coders working in frank companies and many more shows like that you will also have an option to interact with industry leaders on the platform and get a chance to take part in an online work interview to sharpen your skill and get real-time feedback and you get all this adjust rupees triple nine for the entire year and if you use my code jacket then you will get 10 extra discount on this um subscription here is one more show where our sugar will talk about the different ways in which a student who is not studying entire uh you know tier one college can get placement in a good company so in first episode he'll be covering importance of projects different technologies and tech stacks resume from you know resources from where you can learn types of questions asked in interviews about projects and model answers so you can check out the details of the show using the link below in the description box of this video and don't forget to use my code jkl10 to get 10 percent extra discount on your subscription so now let me create a file string first dot c right then we use header file okay main function sorry and here first of all we declare a string suppose i am taking string is name and here i am taking 30 characters right so first thing here you can initialize the string like i'm writing a chain that's it and i want to print using printf the format specifier is percentage s and you just have to provide here name of the string that is name and i guess that's it now we can run it and let's see what happens it should print jenny on screen right see it is printing jenny right and if here i'm writing here khatri so let me just run this again it is printing jenny khatri because we are initializing here right so that is one way or you can initialize character by character right that is another way something like this like this but here we will provide explicitly what null character because a string would always be ended with a null character and let me just save it and run it see it is printing journey right so i guess that is fine now we will initialize the string at runtime i am not initializing here i am just using printf so this kind of function right so at first it should ask enter name right and now i'm using scanning format specifier and here we don't need to put any for loop and address of operators directly you can write down the name of the string because it is pointer to the first character of the string so i'm just using scanf and then i'm printing whatever i'll enter i'm just printing that thing so i guess that's it now let me just run it again so it is asking enter name and i am entering j e double and y and see it is printing journey right let me just run this again and here i want to print j e double n y space khatri now what output you will get only jenny see it is not taking khatri it is only printing journey because scanner will not consider this white space in string that we have discussed right if you want to take input both like this a complete line or you can say then we use what get is or second way is what you can use here what two percentages like if i'm using here percentage s name suppose i'm f name i'm taking two string obviously you have to take and last name right so here also we use what care name f name and l name that is also suppose 30 right and here i am printing f name and last name so here also what percentage s so i think that's it let me run this so it is asking enter name and i am entering jenny space khatri now it is printing jenny khatri but with no white space why because in this printf percentage is percentages so here you can if you write space if in printf you will you know just write or press space here then it will print something like this it will print with space right obviously we don't want this thing so that is why we use what get s i'm just you know taking what back to this program 30 enter name and rather than scanf i'm just command commenting this out both printf and scanf right and here now rather than scanf i am using get s and what are the syntax directly you pass the name of the string get name right and printf percentage s comma name directly you can print something like this right so now let me run this here i am entering suppose here jenny's lectures is best channel oh csid printing the complete line jenny's lectures is the best channel for csid right because we are using gate s so when you press enter or the new line then only it will stop reading but here i have shown you the problem the the that buffer overflow problem we have discussed with both scanf and gates see care name the size is only 30 30 character i mean we should enter 29 character and one null character at last that would be appended automatically right but here characters are obviously more than 30 here so but but still it is printing this thing still it is going to read the complete string it is not giving any error any warning or anything so i think we should not use this kind of thing we should not do this kind of thing that is why get s is very risky to use because it is going to allocate a memory it is going to store this complete line beyond the located memory and maybe it can override some critical program that is very helpful for your pc right so that is a buffer overflow problem i guess you voted both with scanf also will do this thing if you write here scanf right and without space if you write complete line journeys lecture is the best and for csit like this without any space obviously that if you uh you know press any space then it will not read but without space if you write until you uh you know press enter till then it will try it it will try to read the complete line so that is buffer overflow problem here so now see we will see now what printf and put is now here i am using putties and what's the syntax here just have to pass the name to put s i'm using then you will get the difference okay so output should be three names it should print one is printf then two putters i'm using so now let me save it and run it see it is asking enter name so i am entering here janti khatri right with space and because you are using gates so let me just press enter here and see three times first jayanti khatri then second and then third is in next line why so because first chanti khatri is output because of the sprint f next output because of this protest in the same line and protest is what potus will automatically add a new line at the end of the string while you are printing right means after printing the entire string the cursor would be in next line so the output of next protest would be would be next line i hope you got this point also right now next thing is if if you do something like this like i am printing here point uh 5 only 0.5 means i want only 5 characters first 5 characters of my name and next time printing printf 10.5 right yes filled with also i am specifying here and that is name but i think better to use here what slashing and here also slash and then you will get it better all right now let me just run this and here i'm entering see first is j-a-y-n only five character because of this printf second print f ten filled with and only five characters that is why that is and that's what it right uh you know justified so this output and this is because of the potence so i hope you got this also right and if you want to take input using scanf one thing you can do with scanf something like this like if i am writing here percentage 5 yes right and here i am passing name so now what output you will get see i mean jayanti and without space khatri right or maybe jayanthi only seven characters we have right and i'm pressing enter but still it is printing j y and only five and see obviously this will print only five because we are using point in in both the printer but put s should print jnt but it is also printing jy ny so because we have specified in scanf percentage 5s means it will read only 5 characters whatever you will enter after 5 characters it will not read so that limit you can set here you know scanf this thing also you can do right one more thing what you can do in printf one more printf i am using here and percentage s and here i am specifying address of from index second name 2 means what output you should get here in this case see i just want to run this and i'm writing here a one slash i'm just commenting out this potential and protest i hope you go to the working of this and this printf also and this printf also and in scanf or not scanf i'll use get s rather than scanf right only one output i want here so now let me just run this and it should ask name that is jaintikhat3 enter see what it is printing it is going to start printing from y because of we are providing address of name of 2 address of second index 0 1 2 right so it is not printing whatever in at a 0th index that is j at one index that is a at second index it is y so address we are providing from y so from y it is going to print and till what till it finds null character that is till khatri i hope you got everything about gator scanf and printf and potus right so that's it for this video now
Info
Channel: Jenny's Lectures CS IT
Views: 142,843
Rating: undefined 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
Id: E00P5cZfnFQ
Channel Id: undefined
Length: 14min 39sec (879 seconds)
Published: Thu Jul 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.