 Hello, hi everyone. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So today we are having day 18 of C and C++ coding bootcamp. And in today's session I will be discussing about string in C programming. So before starting the session, I just want to show you the session tracker and the sessions which we have conducted so far. So you can see I started this bootcamp in June and so far we have completed 17 sessions. And this is the list which is available in front of you. And these sessions we have covered so far. So if you have not followed all the sessions, so you can. And in the middle you can see I just provided some pre-recorded video links as well where lots of practice exercises are available. So today we are having day 18. And in this session I will be discussing about strings and C programming, right? So moving on to the topic. So before starting the session, I just want to show you a few things about me. So if you don't know about me and you want to know who is your instructor, so this slide will help you to know about me. So I have 16 plus years of experience. I have worked in both education industry and IT industry and helping students, freshers and professionals so that they can ramp their career in Salesforce ecosystem or you can say in IT world. So moving forward, consistency is the key. So please do regular practice. Without practice you won't be able to understand programming. And this is self-help group for this coding related stuff. So you can join this and you can ask questions here. And if you want to receive timely notification, you can follow Sanjay Gupta Tech School on YouTube, LinkedIn, Instagram and Telegram. And all the important links are available in the video's description. Okay. So with this note, I just want to start the session for today. So today's topic is string. String is very much important and this topic is available in C programming, C plus plus programming, Java programming, as well as in Apex programming. So everywhere we use strings, right? And strings are basically collection of characters. Okay. So as you can see here, few points related to strings are written. So we will be discussing these first of all. And in simple terms, if someone asked like what is string, so a string is a collection of one or more than one characters terminated by a null character. So this null character is important in C programming. So in C programming, if you are creating any string and it is having more than one or one characters, so at the end, you need to provide this backslash n, which is important. And we have one terminology which is known as ASCII value. So ASCII stands for American Standard Code for Information Interchange. So this null is basically a character backslash zero. This backslash zero null characters, ASCII value is zero, right? So whenever you will be creating any string, so this null will be available automatically, you just need to know like null is required at the end of the string, right? And its ASCII value is zero every time. And string constants are enclosed in double quotes. So whenever in any programming language, like if you provide any string, then we use double quotes. And in some programming languages, we use single quotes as well. So in Apex, basically we use single quotes, but in C programming, C++, Java, for string constants, we use double quotes. And a string can contain alphabets. Alphabets means uppercase a to z or lowercase a to z, then we have digits from zero to nine, and then we have spatial symbols. So other than alphabets and digits, whatever symbols are available on keyboard, those are spatial symbols, and they can also be part of string. So combined alphabets, digits and spatial symbols, all these character combination becomes a string, right? So this is overview about string. Now, let's understand how in C programming, we can declare a string, right? So with the help of this slide, you will be able to understand how we can create a string, right? So here declaration is mentioned. So for creating string in C programming, we use data type care, right? So in other programming languages, you might be using string directly string as a data type. But here in C programming, we have care as a data type. And then we need to provide name of the string, right? And the size size is also important, like how many characters you will be storing. Okay. And at bottom, you can see the example we have data type as care, then name of string is str and size is 10. So it means this string can store 10 different characters. So if size of string is 10, then it can only contain nine characters, because null also occupies one byte. So as I told you in the beginning, if you are creating a string, so at the end of string, null character will be available, right? So if size is 10, so total nine characters will be there and at the end, null character will be present. Okay, so this way, we'll be able to create the string. Now how its memory location look like. So I hope you have gone through array related sessions, which I delivered in last week. So I explained to you how we can create one day array, how we can create 2D array. So string is basically similar to one day array, right? So in one day array also in one row, we store similar type of values. So string is kind of one day array, right? So here you can see we have care str 10. So this is one day notation, but here we will be storing characters and all those characters will be terminated by null. So that's why this character array is spatially treated as string. Okay, and memory location, you can see total 10 blocks are available. And at the bottom, you can see indexes are also available, because in array, every location has a unique index, which basically starts from zero, and it ends till like total number of size minus one. So if sizes 10, so last number of index will be 10 minus one. And each character occupies one byte of memory if we talk about C programming specifically. So above string occupies 10 bytes of memory. Okay, so this is very important to know like whenever you are creating any character array that you will be using as a string. So how its memory location look like. So it is similar to one day array. And this way you can assign values to character array or you can say string. So first declaration is s one s one is character array sizes five, then we are assigning a b c d e a and all the characters are enclosed in single quotes, then as to all the characters are enclosed in single quotes. But at the end, we have null character as well. So if you store null, so number of elements will be less. Because one location will be occupied by null character. And third declaration is basically having double quotes. So in double quotes, you can put any number of character basis on the size. So sizes five, so number of characters are four, because fifth location will be occupied for null character. Right? So here one by one explanation is available. So s one is basically array of character. The first declaration will be known as array of character because here we are not assigning null, right? s two is string because it contains null at the end. But it is very difficult to initialize in this way because for each character, you are putting single quotes. So to simplify this, we have third notation. So s three is using double quotes. And here null is inserted automatically. So it is also a string, right? So third one is the suggestive way. Whenever you want to store a string, so just put double quotes inside double quotes, you can put number of characters basis on the size of your array. And last index will be for null. So this way, third notation is the beneficial one. So I hope with this explanation, you are able to understand how we can declare string, how memory is allocated, and how we can assign values to strings. Now, if you want to read values for string from user, so we have two different ways. So first one is string, sorry, first one is scanf. So through scanf, if you use percent s, so you will be able to read values from user, right? So all the values will be stored in the string which you declared. So here, if you read the explanation, it says this statement reads sequence of characters from console. Console means your screen where you provide some input until user processes space or enter key, right? So whenever you press space or enter key, so reading will be terminated. This is important to understand. So this statement automatically inserts null character at the end of string, right? So whenever you are entering some characters, so you just need to enter the characters, null character will be inserted automatically. And next we have GATUS. So GATUS is another way to read string. So this statement reads sequence of characters from console, console again from where you provide some input or see the output until user presses enter key. So here, if you press space, so that will also gets input, right? So it will be stored. So if you want to terminate the input, so you need to press enter key only. And the statement automatically inserts null character at the end of string, right? So this way we have two different ways. We have two different ways to read string values from user, right? And I will be using these methods in the program that I will be demonstrating going forward. Now, after reading some values into string, there may be chances you want to print whatever values are available in the string as output. So we have printf with %s, we have put us with str directly, and we have a loop. So as we are discussing about string, and initially I told you in string, whenever we put some character, so at the end, null character will be available, right? So here we are iterating a loop which is starting from zero, and it will iterate till null. So if str of i is not equals to null, then this loop will be iterating. If it is equals to null, so it will be terminated. So basically what is happening, this loop is printing character one by one through %c. Okay, so these three different ways we have if we want to display the values which we have in string, right? So I hope you understood whatever I explained. If you just want to revise so you can just go backward and watch the video again so that you can understand whatever I have explained so far. Okay, now after understanding the basics of string, after understanding basics of string, we need to know how we can implement the program. So here you can see for today's session, I have four programs that I will be implementing for you, right? So all these four programs I will be implementing practically and you will be able to understand how string related programs we can implement in c programming. And this is very much important to know. So here we have first question which says find out length of a string, right? So length of a string means we need to count how many characters are stored in a string. So for that, I'm moving to online compiler. Okay, so here first of all, I am creating one character array, its size is 10, right? So initial size is 10, but we doesn't know. We don't know how many characters will be provided by user because here I'm writing enter your, let's say, first name, enter your first name and I'm using get us str, right? So what is happening? String size is 10, but you doesn't you don't know how many characters will be provided here? Okay, because we are using get us and you you will be just typing your name and we'll be pressing enter key. Now we need to count how many characters are there. So I'm creating one count variable, which is zero and I then I'm starting a loop from zero, then I'm writing str of I not equals to null. And then I plus plus. Right? So understand this loop carefully. As you know, whenever we are working with a string, so string will be containing some characters. And at the end of last character, we will be having null characters. So this is the way we can represent null character using backslash zero, right? So if str of I not equals to null, then we will be counting this variable plus plus count plus plus count will be incremented by one every time. Right? So I hope you are able to understand this. So I is starting from zero, then str of I means str of zero index. If zero index is not equals to null, then count will be incremented. And then I will be incremented. So I will become one. So str of one, next time in second rotation, this loop will check str of one. So if str of one index is not equals to null, again, count will be incremented. Right? So before null, whatever characters we are having in a string, all those will be counted and the value will be available into count variable. And once this loop completes its execution cycle, then we can display the value of count. So I can write count equals to percent d and then count, right? So number of characters will be counted in this count variable, and that will be displayed as output. Okay, so initially we declared some variable, then we received value from user, then we applied some process through this loop so that we can count number of values. And then through this printf, we are showing the result on output. Now what I'm going to do, I'm just clicking on run. So it is starting its execution. Yeah, so here some warning is available, the gattus function is dense rush and should not be used. Why so? Because here size is 10 and gattus is not checking total size, but it is a warning. It is not an error. Right? It is a warning. It is not an error. So here I'm typing my name Sanjay and pressing enter. And you can see count is equals to six. So this way this code is working fine. Whatever number of characters I entered it counted it correctly. Okay, so I hope with this demonstration you are able to understand the first example. Now what I'm going to do, I'm going to implement solution for another that is copy a string into another string. So I will be having two strings. One string will be having data that data I need to copy into another string second string. And then I will be showing both the strings and we'll see whether they are having same data or not. Okay, so this this will be interesting to implement. So here I'm having s one of 10 and s two of 10. So this way two strings are declared. And I think count is not required here. We will be using variable I only. Then here I'm entering first name into s one. So s one is actually having some data. Now this loop will also iterate on s one loop will also iterate on s one because data is available in s one. And when we iterate this loop, and if loop is on like loop is iterating and if it is before null, so data will copy if s one is now on null character, then this loop will be terminated. So what I'm going to do I'm going to write s two of I equals to s one of I so generally in assignment what will happen data of string one will copy into string two right data of string one will copy into string two biggest string one is written on right hand side right hand side value will get copied into left hand side. So that's where it is written. Okay, and I will be incremented. So condition is totally dependent on s one because here you can see we are reading data into s one only. So basis on s one this loop will iterate. And once null character is available. So this loop will be terminated. Now there is one question that we need to focus when this s one will be on null character. So this loop will be terminated. So this means all the characters from s one will copy into s two except null character, because when s one I indexes on null character, then this loop will be terminated. Okay, so what we need to do we we just need to write s two of I equals to null. Right? So at last index of s two, we need to explicitly store null character so that it can become a string. This is important. Okay, so this this loop is for copy. We are copying data from s one into s two. And all the characters will be copied except null, because at null this loop will be terminated. So after completion of this loop, what is happening? We are assigning null into s two explicitly. Now I am going to show values of both the strings. First time showing s one. Now I'm going to copy this pasting it going to show s two. Right? So this way both s one s two will be displayed. But input we will be providing to s one and s two will show the same value as we have in s one. Right? So this way, with this code, your string will be copied into another string. So I'm clicking on run. It is asking string. I am entering Sanjay. And here you can see s one and s two both are showing Sanjay on output. And I entered Sanjay into s one only. But s two is also having the same data. Right? So this way, with the help of this loop, I was able to store the first string content into second string. Okay, so I hope with this demonstration, you are able to understand. And if you are watching this session, maybe live or recording, so do practice. If you are BTEC student for you, these videos are very much important. And if you are from non technical background, you want to learn programming. So for you also, these sessions are very much important. So do practice so that you can also become expert in programming. Right? So next is count how many wall or consonants are available in a string. So now we need to apply some if condition so that we can count accordingly. So right now I'm going to solve question number three. So in question number three, we need to count how many wall and consonants are available in a string. So let's see how I will be solving this problem. So from here, I'm going to remove this. And I need to count wall and consonant. So here, I'm using two variables, we and see both are initialized with zero. This loop will be same. And here I need to put some logic and rest of the code I'm raising so that I can implement the logic first. So this loop will be same because we need to iterate the string until null is available. So before null, whatever character is available, we need to check whether it is wall or consonant. So we will be checking walls because total five walls are available. Consonants are 21. So instead of focusing on constants, we'll be focusing on walls. And if we identify whether characters wall or not. So if it is wall, wall variable will be counted. If it is not, then consonant variable will be counted. So here I'm writing s one of I equals to a or s one of I double equals to e or s one of I double equals to oh, sorry, oh, then in next line and writing. So if you have space, you can write in single line as well. So s one of I double equals to I s one of I double equals to you. So total five conditions are here. And in between I'm using or because or says if anyone is true, then it will be considered as true. So in that case, I will be incrementing V plus plus else C plus plus because if string index element, the character which is stored, if it is equals to either a or e or I or oh, or you, then it is wall, otherwise it is consonant. So this way, the code is implemented in front of you. Now once this process is completed, we need to display wall and consonant. So I'm writing printf wall equals to post in the V, then again printf backslash and consonants equals to post in D and C. So this way, number of wall and consonants will be displayed. Right now I'm going to run this code. Again, I am entering my name so that I can see number of wall and consonant. So here you can see in my first name, Sanjay, there are only two walls that is a and a and four consonants are available. Right? So I hope with this demonstration, you are able to understand how we can count number of wall and consonants. So do practice for this and you can see in this for loop also, we are using conditional statement, we are using logical operator. So whatever we have learned so far, everything we are using together to implement solution for a business problem. Right? So I hope with this demonstration, you are able to understand the concept of string that is used in C programming. Now the last question, which is also very interesting, we need to count occurrence of a particular character in a string. So occurrence means how many times particular character is available in a string. Okay, so what we'll do will be first reading the string, right? Then we will be reading what character you want to count, right? So whatever character user will be entering that character will be counting like how many times that character is available in the string. Okay, so now I'm going to modify this. So I'm keeping C equals to zero as is and here I'm creating one more variable CH. Then here I'm reading and the character you want to count then scan F, percent C and ampersand CH, right? So first I will be reading the first name, then I will be reading a character like the character, how many times that is available in the string. And here I will be applying the logic. So loop will be same because we need to iterate the loop till null character. So until null is available, loop will process. And here we will be writing S one off I double equals to CH. So S one off I is the string character. CH is the character that we want to search. And if both are matching, so we can say count plus plus. So whenever this if condition is true, so count will be incremented by one. So this way we will be able to identify how many times this CH variable is available into this S one string, right? So this way counting will be done. And at the end, we'll be displaying total number of count. So here I'm writing count equals to and then see and this printf I'm raising. Okay, so this way this program solution is implemented clicking on run. Again, I'm entering Sanjay and it is asking number like num it is asking for the character. So I entered a and you can see in my first name Sanjay number a sorry character is available twice. So it is counted as two. So this way, whatever string you will be entering. So in that string, whatever character you want to count, you can read in this CH variable and basis on this condition that character will be counted. Right? So I hope with this demonstration, you are able to understand the working of string. So do practice whatever I demonstrated in this video. And please provide a review or feedback. And I also created one exercise sheet. So it is updated to the latest. So this is exercise sheet for this C and C plus plus coding bootcamp. So in all the sessions, whatever examples I demonstrated, all are consolidatedly available here. So daywise, if you are a beginner, you want to learn programming. So you can just follow the sheet. So live session link is available followed by the practice questions. So you can watch the session and then you can practice, right? So till 15 16 17 day 17. So today's session I will be updating tomorrow. So day 17 till day 17. As of now, all the live session links in practice exercise is available. And as I will be delivering more sessions, so all those will be available here also. Right? And the topic list is available in the session tracker that you can follow. So all the session details are available here. So if you follow, you will be able to understand. So like for beginners, C and C plus plus programming is very much important. So that's why please follow this bootcamp, do regular practice so that you can understand the concepts very well. And in C plus plus Java, A picks programming, whatever we'll be learning here will be beneficial for you. Okay, so with this note, I take your leave. And tomorrow I will be having one more session on strings. So do join that. And in upcoming sessions, I will be covering more topics. And soon we'll be starting C plus plus related sessions as well. So keep following the sessions and share this information with other friends as well so that they can also be benefit. Okay, with this note, I take your leave. Thank you so much for joining this session live. And if you're watching the recording, thanks to you as well.