 Hello friends. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to explain how you can find out length of a string. So remember one thing, I'm not going to use any predefined function of string to identify length. Instead, I'm going to use a loop so that you will understand how we can iterate a string so that we can visit its individual indexes and we can identify how many characters are available in a string. So before we start, just one information. If you want to watch more programming related videos, so you can go to detail or description of this video where you will find various links of playlist related to C-language. So you can follow them. Now I'm going to start explaining this problem. So let's say we have a character array in form of string. So these are the different blocks or indexes of that string. So suppose its name is str and it is having these values s, a, n, j, a, y. So this is my name and at the end it will be having null character. So you all know if we are working on string. So string automatically terminates without null character. So null is represented with backslash 0 and these are the indexes. So after null, there is no data available. So let's say total 10 indexes are available there. So we have 1, 2, 3, 4, 5, 6 values. On 7th position we have null and 3 positions are blank. So in our program what we need to do, we need to count how many characters are available before this null character. And C provides a predefined function for that that is known as str-alien. So you can use that function to identify length of a string but in this video I'm going to use how str-alien works internally. So that you can implement your own so that you can iterate this string and identify its length. Now I'm going to write the program. So first of all we need to declare a character i, that is scan str, size is 10. Then I am declaring two, eighties are available. First is i and second is p0. Then to read input from user, this printer will be used so it will be printing the message, enter string on output screen. Then to read the string I'm using gattus. You can also use scanf in place of gattus. So gattus reads our spaces as well whereas scanf doesn't. Now after this I'm going to repeat a loop. So this is very important. So here I'm putting i equals to 0 as initialization. Then I'm writing str of i not equals to null. str of i not equals to null. So this is termination condition. And then I'm putting i++ or increment of loop. And inside this loop I am writing c++ which is the counter that is initialized as 0. Now I am closing this loop. After completion of this loop I can print length equals to %d and counter variable c. So now I'm going to explain you how this loop is working and how after completion of this loop you can get the specified length as printed. So starting from 0. So i is initially 0. i is initially 0. Now check this condition. It is str i. So str 0. So we are going to check 0 index value. So str 0 is having s. So now we need to compare s not equals to null. So s is equals to null or not. No, it is not equal. So it means condition is true. It means c++ will be performed. So initially c is 0 and right now its value will be 1. Now i will be incremented. So its value will also be 1. Now you need to check str 1. So str 1 is having 8. So a is not equals to null. It is again 2. That means c will be incremented. So it is now 2. Then i will be incremented. So i is also 2. Now we need to check str of 2 position. So str of 2 is having n. So n is also not equals to null. So again this condition will be true and c will be incremented. So this time value of c is 3. So this way right now you can see value of c is 3 when we are on this position. Then i will be 3 and c will be 4 because at 3 position we have j which is not equals to null. So c will be incremented. So it will become 4. Then i will be 4. We need to check this position. It is not equals to null. So c will become 5. Then again i will be incremented. So it is 5. So at 5th index we have y which is also not equals to null. So again c will be incremented. So now its value is 6. Again i will be incremented. So this time i is 6 and at 6th position you can see we have null character. So null not equals to null. So this time this condition is false. Null is actually equals to null. So if it is false then we need to come to print as which is printing length equals to percenty with c variable. And here you can see c variable is having value as 6 that will be printed on output screen. And we have 1, 2, 3, 4, 5, 6 characters in this string. So this way with the help of this loop and with this condition we are able to count how many characters are available in the string. So this way you don't need to use str alien function. You can manually iterate a loop till null character and you can identify how many characters are available in the string. So sometimes an examiner will ask you to implement this approach to identify length of string without using any predefined function. So in that case you can use this logic. So i hope you understood whatever i explained in this video. If you want to watch more programming related videos you can go to playlist on my channel. Or you can go to detail or description of this video so that you can directly find links of playlist related to c programming. So do watch them and i hope you enjoyed this video. Thank you for watching this video.