 Hello friends! I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how you can compare two strings in C language. Before starting, I want to give you one information. If you go to description of this video, you will find links of various playlists related to C language. So now I am going to start explaining how you can compare two strings in C language. So first of all, I am going to read two strings from the user. So let's say string 1 is S1, whose size is 10. String 2 is S2, whose size is also 10. Then I am going to compare one variable i and another variable f that will be for flag which is initially 0. Now to read input from user, I am writing a printf which will display enter first string. Input will be stored inside S1. Then again printf enters second string. Then getters S2. So this way, first string will be stored inside S1 and second string will be stored inside S2. So now I am going to implement the loop and through that loop, we will see how we can compare two strings whether they are equal or not. So remember that I am going to compare both the strings as per their content, not as per their length. So for example, this is S1 or this is S2. We have two strings. So here you can see both are equal because their contents are same. If S1 is X, Y, Z and S2 is A, B, C. So these two strings are unequal because their contents are unequal. If length is same like S1 is having three characters, S2 is also having three characters, then based on their length, we are not going to compare. We are going to compare as per their content. So I will use these two examples while I will be implementing the loop. So after implementation of loop, we will come again on these examples and we will see how that loop is working to check the string comparison. So I am starting for loop from zero. Now here condition is S1 of I not equals to null or S2 of I not equals to null. So here see the condition. S1 is checked, then S2 is checked. If S1 is not equals to null, if it is true, then second condition will not be evaluated because I have mentioned or in between them. And if this condition is false, then this will be evaluated. If it is true, then also loop will iterate. So if any one of these two conditions are true, then loop will be evaluated, otherwise it will be false. So if both the conditions are false, then loop will be discontinued. And after here, I am removing this. So here I need to write I plus plus and then inside loop I am going to write the logic. So I am writing if condition, if S1 of I not equals to S2 of I, if S1 of I not equals to S2 of I, then I am assigning one into F and I am using break so that I can terminate the rotation of loop and I am closing the loop. So this is the simple loop that I implemented for still comparison. After completion of this loop, I just need to check if F is still 0, then I can print equal. If F is equals to 0, then I can print equal, else I can print not equal. So now I am taking this example first so that we can check whether this implemented loop is working fine or not. So initially you can see F is 0. Initially value of F is 0. Now we need to check this loop. So I started from 0, then S1 of I not equals to 0. So let's say I is 0. Now check S1 of I not equals to 0. So you can imagine that this is 0 index, this is 1, this is 2 and then on third index both are having null. So S1 of I not equals to 0. So S1 of I is not equals to 0. It is true. So if first condition is true, second will not be evaluated. It means loop condition is true. Now we need to check S1 of I with S2 of I. If it is not equal, then F will be 1 and loop will be eliminated. But in this case, both are equal. So this condition will be false and I will be incremented. So now I is 1. So here you need to check S1 of I. So S1 of I is B, which is also not equal to 0. So in this first condition is true. So second will not be checked. Now check this condition S1 of I not equals to S2 of I. So again it is false because both are equal. So I will be incremented. So I is 2. Again check this condition S1 of 2 is having C, which is not equals to null. So again this condition is true. Check this condition S1 of I not equals to S2 of I. So again this is false because both are equal. So I will be incremented. This time I is 3. So check this condition S1 of I. So S1 of 3 is having null. So this is false. Check this condition S2 of I is not equals to null. It is also false. So both conditions of loop are false this time. So you will be incremented and you saw we never born inside this condition because every time it was false. So I have remained 0 and we can say that both the strings are equal. So this is the first scenario. Now let me take another one, which is this. String 1 is XYZ and string 2 is ABC. So again I am starting I from 0. So S1 of I, S1 of 0 not equals to null. So this condition is true. So setting will not be evaluated. Now come here. S1 of I not equals to S2 of I. So S1 of I is X. S2 of I is A. So X not equals to A. So first time condition is true. So it means F will be assigned as 1 and break will terminate the loop and after termination of loop we need to check if condition. So this time F is 1. So it will print strings are not equal. So this way this is working. Now let's say if you have this kind of scenario. In first string we have AB and null and in second string we have ABC and null. So what will happen? So in two rotations while I will be 0, so both are equal, while I will be 1, then B and B both are equal. While I is 2, we are on this position. So for string 1 it is null. So this condition will be false. So this condition will be checked. So it is true because S2 is not equals to null. So it will come to this condition and here inside this condition S1 of 2 position is having null. And S2 of 2 position is having C. So here you can see this condition is not true. So F will be assigned 1 and loop will be perfect. And after termination of loop F is 1. So not equal will be this way. So this way if all the characters of both the strings are equal then only this if condition will be false. And whenever both the indexes are unequal then automatically it will become true and F will be assigned as 1. So this way I explained you how you can compare two strings. So I explained this example with the help of three different strings. So S1 S2 I used three pairs of S1 and S2 to explain this loop how it is working. So don't confuse yourself like why I used all here because I explained you with the examples like how it is working. So if first condition is true we don't have to check another one because here we have a comparison condition. So don't put end here just use on so that we can check each and every index of both the strings. So if any point if indexes are unequal F will be having 1 and loop will be terminated to break and then we need to check if condition like F is 2 or 1 and accordingly also will be this way. So this way I hope you understood how we can compare two strings in C language with the help of this example. And if you want to watch more programming related videos you can open my channel go to playlist where you will see various playlist related to C language. Also you can find those links handy in the description of this video. So do follow those videos so that you can learn programming in a minute. And I hope you understood whatever I explained in this video. Thank you for watching this video.