 Welcome back. In the last segment, we discussed how text is stored on a computer and in this segment, we are going to talk about functions that operate on character strings and also the notion of character string constants. So, there is a standard rule for passing arrays to functions. So, what is that rule? You pass the array name and the value of the array name is simply the starting address of the array. So, when you pass the array name, you are telling the function where the array is beginning in memory and then you pass the array size. So, using these two things, the function can figure out how many elements the array has and then this function inside the function also you can do the address calculation to get at the different elements. And the array size, well, you can check, you can look at the array size to decide what is reasonable or what is a legal value of the index. The rule for passing char arrays to functions is slightly different. So, we have to pass the array name. So, the function has to be told where the array starts in memory from which address the array is allocated. But there is no need to pass the array length because of our convention, we only process still the null character. So, we do not really worry about the length of the array in any case. So, therefore, there is no need to pass it. So, again, to reiterate, we do not look at the length of the array, but we keep processing until we encounter the sentinel which is the null character who that is the character whose ASCII value is 0. And that tells us that, oh, the array has ended over here. So, let us write a simple function for on strings. So, this is a function which determines if a character X is present in a string CSTR. So, it takes as argument the character X itself and character array CSTR. So, we are going to call it constant because inside this that array is that CSTR is not changed. As said in the title, the purpose of this function is to determine if X occurs inside CSTR. And so, it will return true if X occurs and return a false if it does not occur. So, how does this work? So, we are going to start scanning the CSTR from index 0. So, i is going to be our index. And so, we are going to write nt i equal to 0 CSTR of i. And the condition now is until we encounter the null character. So, the for loop has this condition and often the condition deals with the length of the array, but in this case, we will just say that look i the element at index i should not be a null character. And then the update is the same as before. So, we increment i, we go to the next index or we scan forward. And here if we encounter X, if for if at any index we encounter X, then we return true. If we go through this entire loop and we do not encounter X, that is when we will come out of our control will come to this point. So, if control reaches this point, then that means X does not appear in this string CSTR. And so, we return false. So, here is how this program could be called through a, could be a function could be called through a main program. So, we are going to set and set a character array to India. And then we can say find care D in the name. So, in this case, D does appear in the string India or in the strings stored in name. And therefore, they should print this will print a one. As you may remember true is not printed, but one is printed instead. And if you try say to search for a C, it will not be found and therefore, we will print a zero. So, let us do a quick demo of this. So, here is the file. So, I am going to compile it now and then run it. So, as expected one was printed followed by zero. So, here is another piece of code which is very similar. And I want to ask you whether you think this should also work. So, in the previous program, what we did was we put the string into an array and then called find care. But you would think or you would desire that something like this should also work. Why should we have to put that string into an array and then call it? Why cannot we send the string off directly? And similarly, if we try to find C in India that should print into zero. Well, this does work. And what we are going to do next is to see how or why this works. So, for this we need the notion of a character string constant. So, character string constant, constants are simply text enclosed within double quotes. For example, India is a character string constant. C++ is nice and please give your name are also character string constants. They are constants in the sense that they are not variables. You cannot do, you cannot change things inside this. You are not expected to change things inside this. Now, how does C++ deal with such constants? Well, first of all, C++ stores the string in some place in memory. And, of course, a null terminator is also stored. So, that is a convention. And sort of it is useful to follow conventions sort of blindly. So, the null terminator is used and you will see that it comes in handy. And now this resembles a car array. So, C++ denotes a character string constant by the address where it starts. So, it really treats it like a car array. But it is a car array that C++ itself has created. So, when you type this, C++ looks at it and it stores it somewhere and it says that, oh look, whatever you have typed here is at this address. And it is the array stored at this address. So, when you pass the character string constant, the address where you stored it in memory is passed. So, this happens just like any array. And I should note that the type of this is const char star. So, what that means is you cannot modify the content of this inside the function. Rather, I should say you will not modify. You are promising that you will not modify the array that is being passed. And indeed, C++ does not want you to do that. So, if you send, if you write, if you send this as an argument to say find char, then C++ does not want you to change the value of this C++ is nice, this entire thing. There are reasons for it. C++ may store it in regions of memory, which the computer may have some special restrictions on. So, that when the user program executes, that region of memory is read only, that may well be possible. So, as a result, C++ says that look, you are not supposed to change this. And the way C++ make sure that does not happen is by requiring you to declare this argument as const. In any case, you do not, you are not intending to modify that argument. When you try to find the character in some string, you are not intending to modify that string anyway. So, there is no harm for you in declaring it as a const. So, that was a relatively simple function and that was a discussion of what are character strings. Now, we move on to a little bit more complicated function, but more than the complexity, it is a new notion that we want to introduce. This is a notion that is very commonly used with character strings, that is comparing them using the lexicographic or the dictionary order. So, what is that? Well, words are stored in the dictionary in a certain order that makes it easier to locate the world if needed. You have, you know this, when you search a dictionary, what words come first? Well, all the words beginning with A come first, then those beginning with P, then those beginning with C and so on. And within the words that begin with A, what is stored first? Well, those with second letter A, then those with second letter P and so on. So, this is the so called dictionary order of words or the lexicographic order of words. So, the function that we are going to write has the following specification. So, it takes two character strings and returns equal to if the strings are exactly equal. So, it returns the character equal to. It returns the character less than if the first string will appear before the second in the lexicographic order. And it will return greater than if the first string will appear after the second in the lexicographic order. So, this is an order and this is an order between words and you can say that something appears before, so it is smaller. So, that is also an implied notion or it is more like an associated notion. So, how do you write this? Well, here is the function. So, let us call it compare and it is going to return a char as we said and it should take two character strings as arguments. So, the two that we want to compare and since we do not intend to modify those two strings, we are going to make them constant. And of course, if we make them constant, then we can also supply as argument character string constants if we wish. So, what is this supposed to do? Well, we will start from the beginning of both and compare corresponding letters. If they are the same, then so far the two strings look equal. You do not know which one will be stored before in the dictionary and which one will be stored later. So, you have to move on to the next letter of both. If at any point you find that A's letter is smaller than the letter of B, then what does that mean? That means that A will be stored before in the dictionary order and so you can immediately return less than. Likewise, if you find that B's letter is smaller, then you can immediately return greater than. If both strings end without finding a, without there being a difference at any position, then you can just return equal to and if one string ends, its letter will be null and null is asking 0 and it is smaller than any other letter. So, effectively this case if you are comparing the two, the corresponding letters, then the letter which is ending will be considered, the letter will be considered, will be smaller and therefore by the previous logic Q will be returning less than. But that is good because the letters which terminate, the words which terminate earlier are considered to be coming first. So, for example, if I write mat that appears before meet, so that is exactly, so that will also be handled properly by our mechanism over here. So, that is really what the algorithm is and so here is the solution. We start with i equal to 0, this is the position from which we compare and while true or we are getting it when we are not, we are always going to the next iteration over here. So, if we find that both the ith element of A as well as the ith element of B are null, then that means we have gone through the loop several times and we have not discovered a difference and we have come to the end of both words and so we can return equality. We have discovered that those words are equal. If we find that ai is less than bi, then as we said we can return less than, if we find ai is greater than bi, then we can return greater than and if none of these is true, so what is that case? So, that case is if ai is equal to bi, then we have to go and examine the next letter and so we do i++. So, this code is also discussed in the book, but this is slightly more compact. So, you are to as an exercise, you have to write a function which takes as argument a character string and returns 0 if the string is 0, 1 if it is 1 and so on and 9 if it is 9 and minus 1 otherwise. So, you can just compare say the given text string with these text strings and depending upon what the comparison is, you can just let return the appropriate numeral. All right, so what have we discussed? So, we have talked about how character strings are passed to functions, then we also talked about how constant character strings are represented and how they are passed to functions, basically you put a const. Then we talked about the notion of lexicographic order and we discussed a function to compare strings by the lexicographic order. We will take a break now and in the next segment, we will discuss two-dimensional arrays.