Hello and welcome everyone….. in this practical module.
And here the way we were discussing different problem statements continuing that flow, we have got here new practical for you where we are learning to develop new concepts and new logics.
So, without any delay we will start and we will see which practical we will be discussing today.
In this video we will be discussing checking Palindrome.
You might have heard this term before or maybe you must have not heard this before.
So, here there is nothing to be surprised of.
Palindrome is a very easy concept, which we can understand in 2 minutes.
And after that we will see what is the exact meaning of checking the Palindrome.
Here if we talk about overview, we would be discussing strings and the strings functions, how on the strings the functions are performed.
So, without any delay we will start and first of all, before seeing all these things, we will see what is the Palindrome? Now we have different words in English and there are such words which if we spell form the start, which means, like you can see that “MADAM” is one word on our screen.
If you read this word MADAM from the left, you would read MADAM.
MADAM is madam if we read it from there left but if I say that I have to spell this word from the right-hand side, I have to read each and every word.
At that time how will I read it.
Even if I want to read it from the right, it will be MADAM.
So, you can see that this is such a word, which we spell from the left or from the right, its letters will spell and sound exact same.
And if we read it from the left, even then its pronunciation would be the same, which we are getting while reading it from the right.
So, this is Palindrome, we call these kinds of words as Palindrome, which means those words which if we read from left or right, their pronunciation is the same and even the letters are the same.
That is why we call these words as Palindrome.
So, you must have got the picture, what exactly we are going to do here.
We will basically check a word or a string, is it Palindrome or not.
Which means, while reading it from the left or the right, same letters come, the pronunciation is the same.
That is what we are going to check in this program, which will be very interesting.
We will also be designing its logic and we will be seeing that how this logic will work here.
For now we have understood that what is this Palindrome.
Now lets talk about its logic.
Suppose what we did is we passed this madam word once again in our program.
Now this madam word has come into our program and we have to check whether it is Palindrome or not.
To check whether it is Palindrome or not.
If you see than we will have to move by directional which means that first we will have to see from left that the 1st letter that comes from
left, is it also coming from the right when we are seeing it.
In this case like it is madam, you will see that when we pick a letter from left it comes as M, when we pick the letter from right, even then it is M.
Here it matches, so now we will go ahead.
After going ahead, we will again see that what is the next letter, next letter is A, if we see from the right, even from the right next letter is A and both of these are same.
Then we will go ahead, now if we see from left, even from left D has come to us and even from the right D has come to us which is a middle point of this entire word.
So, this D and D will obviously prove same.
And with the help of this we will get to know that our word is a Palindrome or not.
So, this entire concept or this entire logic, we have seen it over here.
Now we will go into our text editor and how we can complete this entire program.
How a Palindrome can be checked with the help of C program.
So, here 1st of all we have written the main function and along with that you can
see that we have written 2 preprocessor commands.
First is obviously stdio.h for printf and scanf functions.
Along with that the string.h library that is there, header file, that is also included over here.
You must have understood what we are doing over here.
We are using few strings function, that’s why we have included this header file over here.
So, now we will go ahead and see our main function.
In the main function we will first of all define one string.
In which the user will input its respective word and from there we will be checking whether it is a Palindrome or not.
So, for that we have made a string 1 named string, whose size is obviously 20, like you can see.
And since its data type is string, so it is a character.
Now we have seen how we have made it.
Now if we come ahead of it, we can see that we have made the 2 variables I and length, which are integer types.
Now again we will make use of I variable for the for loop.
But the length variable here, that we will use to find the length of the data in our string.
So, you can see that how this length variable can be used.
Now we will go ahead and consider our flag variable.
So, you must have seen in the last program, when we were checking the prime numbers.
Even there we had made a flag named variable.
When the flag used to be 0, we would say that our number is prime.
As soon the flag used to be one, we would say that here our number is not prime.
In this way we will make use of flag over here to tell that whether our string is Palindrome or not.
Now we will go ahead and see our next step in the same program.
The next step would be taking the input string from the user.
And to take the input we will need 2 functions, printf as well as scanf functions.
So, we have written these functions over here and in this you can see that through scanf we have taken the input in sting 1.
So, now we will go ahead and see that now we have got the string in input.
Now what will the procedure going ahead?
In the procedure ahead we will see first of all the length of our string.
So, we know that STRLEN named function is there in string.h library.
And this function is used to find the length of any string.
So, even here we are making use of STRLEN function.
To find out the length of string 1 string.
Whatever will be the length in the numeric value, that will be stored in what, in the length variable.
So, you have understood now that why we have made this length variable over here.
Now you have understood why we have made the length over here.
Now we have found out the length over here and as we had discussed in the concept, we will take each letter from the left and even from the right we would be considering each letter.
And we will go ahead and check whether both are equal or not.
So, if we have to consider each letter even from the right then it is very important to know the length of the string.
Because if we don’t know that the stings length is 10 and we have reached 15 number and from there we are checking each letter.
Then here the Palindrome will never come out.
That’s why finding the strings length is very important so that we get to know that if the string’s length is 10, so from here we have to consider 1st element and from here we
have to consider 10th element.
So, this was about string length function and why we have found the length of the string.
Now after that we will come to our for loop.
Why for loop is used, for loop is used because so that we consider each and every character in our for loop and we have to check the character on the exact opposite side, whether both these characters are same or not.
So, we have to go one by one, that's why we have made use of for loop.
For loop we had started with “i=0”.
Why? Because we know that the arrays and strings that we have they have the index positions.
Zero index position or five index position and this index position always starts with zero, which means the first element is on the 0th index position.
The second element is on first index, third element is on second index and in this way index go on.
But they start with zero, that's why we have initialised I with zero and what else we have done.
We have done i less than length, now we had found out the length of our string.
If it is 10, we will carry on the loop till then 10, if it is 15, we will be carrying out till 15, if it is 16, we will be carrying out till 16.
That's why we have put the condition over here, the loop should go on less than length.
So, this was the entire condition of our for loop.
Now we decide what we will be writing in for loop.
We will be writing one if statement in for loop.
The logic of the if statement is something to concentrate on in this entire program.
We will understand what exactly it means.
In if statement you will see that we have written one condition, the condition is this way.
Suppose your I’s value starts from zero, so in the first case, the index number of string one we have considered it as zero and it means that the character on the 0th index position we will be using that over here.
And we will check it with the help of not equal to sign which is represented by exclamation mark and equal to sign, is the first operand not equal to the second operand.
Is it not equal? That's why we are using this operator over here and on the other side the second operant that we have put what is that? Which index positions of string one length we have considered here? “length-i-1” so, we have considered this index position.
Now, why we have considered it we will see it over here.
We know that a string is MADAM so we know that MADAM, these five letters which are the part of a string and we have also read that after the sting one null character is automatically put by our compiler, whenever we make one string.
So, basically the string’s length becomes plus one form how many ever characters we have written.
Suppose we have written madam then after M A D A M there is also a null character.
So, here the length of madam string is not five but it is six.
That’s why when we want to consider our last element.
So, what we will do is we will write length-1 and consider the last element.
So, you will see that we have done here “length-i-1” as well.
Why have we done that? If the I’s value is zero if we do here “length-i-1” then only length -1 stays here, which is our last element.
As soon as I’s value becomes one.
I’s value 1 means that we will consider the second element from second last element and to consider the second last, we will have to write the index position to string length-i which means -1 and ahead one more -1.
So, in this way length -2 will be the index position and that will be our second last element.
So, in this way as and when I’s value will increase.
So, in this way we will be considering next element in this place but along with that from behind the loop was going on and it keeps reducing with the help of this logic.
And we are checking over here with this element of the start and the end, they both are not equal.
They're not equal then we will write the flag’s value as 1.
and we will break our if statement and for loop.
Because if we are searching for a palindrome, we want each and every character to be equal from starting & end, if in any situation we find that the starting & end character are not matching then . So, in this case we will put 1 as equal to flag, and we will break the if statement and our forloop. So in this case this is not a palindrome.
So, in this case the flag’s value becomes 1 and break the loop
After that our flag’s value has become 0 and 1, on that basis we have to test whether our string is Palindrome or not.
For that we have put a condition over here, “if(flag==1)”, which we have done here inside the if statement.
In that case, we will have to print that “Our string is not a palindrome”.
But if this doesn't happen, and the flag’s value is not one it stays unchanged.
We have written the flag’s value as zero over here.
So, that's why apart from one, whenever there will be flag’s value, if it happens then in the else part we have written ”the string is a palindrome.” So here the concept is somewhat in this way, we are checking the starting character and the last character whether the both are unequal.
If they are unequal then we make the flag’s value one and we break our for loop because there is no need to go further.
Because we have got to know that our string is not palindrome.
And in that case, we check our if and else condition, whether the flag’s value is one, if it is one the string is not palindrome.
If the flag’s value is one string is not Palindrome, if the flag’s value is not one, if it is zero then we will write that our string is a palindrome.
As and when it will go ahead.
== will considered the flag value will be untouched and unchanged.
Like we had written in the starting flag=0.
In the same way at the end, it will remain 0.
And at the end it will come on the else condition and print “our string is a Palindrome”.
So, this was the entire flow of our program.
Now we will run it and see whether we are getting a desired output over here or not.
So, here I'm writing a word which is MADAM, madam.
We will check it whether it is a palindrome or not, whether looking at it you are coming to know that this is a palindrome.
Then you have understood the concept of palindrome.
What if you're not able to understand whether the string is palindrome or not? So, you can go back again in the video a little and see how does the Palindrome concept works.
So, we will press the Enter here and you can see that it is printed with us.
The string which is Madam, Madam is a palindrome.
In the same way, we will check one more condition, we will take one more thing in which we will use learnvern and see in here.
As soon as we pass learnvern string.
You can see that Learnvern is not a palindrome because if we read it reverse the letters will definitely not be same because our end letter is n and L and n are not equal.
That's why this string is not palindrome.
So, this was about our program where we tested and checked our string or our word that we had passed, which was passed by the user, whether it was Palindrome or not.
So, in this way you can take few more palindromes and with that you can modify the logic and practise this kind of problems and practical. If you have any question related to this,
if you have a doubt, you can definitely go to forums.learnvern.com. There you can ask your query or your question in your language.
We will list the answer there as soon as possible.
Till that time keep practising.
We will meet in the next video. Till that time, thank you so much.
Share a personalized message with your friends.