 Welcome to this session in the course of computer programming. Today, we shall be discussing how to handle character strings in our program. So far, we have seen in the earlier sessions that we can have a variable of type care in which we can store an ASCII value of a character. The ASCII code is actually a numerical value which can also be treated as an integer number. We also discussed how strings can be stored as a sequence of characters in an array. In this session, we will discuss how strings can be meaningfully handled. In particular, we will see how to perform some useful operations on string. In particular, we will look at couple of inbuilt functions which are provided by C plus plus library. It is important to understand that C plus plus actually handles character strings in two different ways. To see why it is so, it is useful to look at some historical context. Beyond Shustra, who invented C plus plus designed this language essentially building on the language C which existed then. Language C does not support objects and object oriented programming. It is classified in fact as a procedural programming language. C plus plus on the other hand was designed to support object oriented programming style in addition to permitting all the procedural features of C language. You will notice that whatever we have discussed in this course so far essentially pertains to the procedural features of C language used in C plus plus. So, let us continue defining and using character string in C plus plus. We have seen already that string can be stored in a character type array. We note that an extra character backslash 0 which is actually a zero value absolute zero value is stored at the end of such a string. Obviously the size of the array which we define to hold a string must contain an additional element to hold this extra value. If the string does not have a blank space within, we can actually read that string directly in an array using our C in statement. The special character backslash 0 is automatically inserted by the compiler at the end of the string read by C. On the other hand, if we form a string inside an array, a care type array in our program by ourselves, then it is our responsibility to ensure that a backslash 0 character is inserted at the end. Here is a program to determine length of a given string. Notice that a string is defined as array word of length 6. Note that this string this array can contain a word of only up to 5 characters because the sixth character position must be used to insert a backslash 0. Of course, the backslash 0 can occur earlier if the length of the string is less than 5 characters long. So, here we read the input string by simply saying C in word. We now scan the string. We are already familiar with scanning an array. We have an index i which varies from 0 to less than 6 increasing by 1 every time. This is the standard for loop construct for iterating around all elements of an array. All that we do is we examine if the ith character or ith position of the array word contains a backslash 0. If it does, the string has ended. We break the loop. Otherwise, we come out of the loop here. Whatever be the value of i at that juncture is assigned to word length. We simply output the length of the string as word length. This program should work properly. We so expect, but look at the interesting behavior. If we execute this program using code blocks, for example, and given input string as hello. The program shows the correct length as 5 because the string hello has 5 characters. However, if the input string is abrakadabra, the program shows the length 6. Why does it do that? Actually, some compilers will work differently and even produce a runtime error. You can guess why. If you go to the previous slide, you will notice that the for loop is running for i equal to 0 to i less than 6. Naturally, because our array size was 6. However, when we input a larger string such as abrakadabra, the compiler, depending upon how it is implemented, might work either terminating the loop execution as directed or giving some runtime error because you are trying to stuff additional characters in an array which is less than 11 characters long. So, the scene command merely reads all characters of the string into the consecutive elements of array word and inserts a backslash as 0 and b s. Now, this works for input string which is less than 6 characters as we notice, but abrakadabra has 11 characters. Now, c plus plus will do one of the two things. In the code blocks, if we put this kind of string as input, it will now put additional characters in subsequent locations beyond the size of the array word. Now, that is of course, pure nonsense because we have not allocated any memory to hold any extra characters, but this is what c plus plus may do. The for loop will however, exit after completing its normal run which is end of i equal to 6. So, value of i will be 6 which will get assigned to word length. Word length will be printed as 6, but the array word will not have a well formed string because the backslash 0 is not available on the 6th position. So, c plus plus expects us to check if array bounds are exceeded. So, our responsibility and not that of c plus plus. A better program is to do this. If i equal to 6, then we say invalid string that means the c in had read a string, but did not perhaps find a string of less than 6 characters. So, it did some fancy stuff, but when we came out i was equal to 6. Now, if even when reaching i equal to 5, I do not find backslash 0, which was the 6th position in the array, then there is an invalid string. So, we return with an error, otherwise we print the length of the string to this word length, but a better strategy would be to always declare an array with sufficiently large size. For example, care word 50, typically the words are smaller than this. Therefore, we are very sure that even if we read a string inside, you read a word inside this array, the word will be properly accommodated. In fact, c plus plus provides a library function to actually automatically calculate the length of a given string. It is called strln. We need to include the corresponding library in our header. So, we have to say hash include c string. The c string library is actually c type of string handling library functions. When we include that, then in our program, after reading the given input string in our care array say word, we can simply say word length is equal to strln word. This will correctly calculate. In fact, it does exactly the same thing that we did in our program. It will scan the word array till it finds a backslash 0 and will correctly calculate the word length and assign it to left hand side. Going further, a sentence may have many words separated by blank spaces or by punctuation marks. The c in statement of c plus plus unfortunately will stop reading input when it encounters a blank space because a blank space or a return key press indicates end of one input value. So, even if I want to read a string, the string cannot contain blank spaces. We need a facility to read complete lines of text independent of whether it has blanks in between or not. Fortunately, a library function provides such a facility. I have indicated here the use of a function called get s string which actually stands for get string. You may want to note that the actual function get s is a deprecated function. However, later on when we discuss file management and file handling, we shall see how the properly defined functions exist to do such thing. Currently, however, get s works on almost all the compilers that we have seen. When we use such a function get a string it will read an entire line up to the end of the line character that we type in. We must of course, include another library called c stdio. This is nothing but our stdio or standard input output stream kind of library except it is a c style library. So, it is called c stdio. This function will read all input characters up to the new line character. However, the new line character itself when it is encountered will be replaced internally in the array string by a backslide 0. So, here is a program to count words in a sentence for example. Imagine that we have written large number of words in a sentence. Hello, how are you for example, each separated by another with a one blank? How do we count these words? Well, first of all we include the c stdio library. Then we define a sentence array which is a care type array fairly long can hold 200 elements actually 199 characters because the last character position you will remember must be reserved for a backslide 0. I ask user to input a string and use the get s function. This get s sentence will get whatever sentence we type as input. Please note that if the sentence is shorter than 199 characters which is what we expected to be then automatically a backslide 0 is inserted by the get s statement. So, we get a well formed string inside the sentence. All that we do now is that we set up an array scan for i equal to 0. If the ith character is a backslide 0 we know that the sentence has ended and therefore, we determine the length as equal to i. Please remember we could also have used the function to calculate the length of a string that we introduced in an earlier example. Next we proceed to count words. All that we do is we scan the array which holds the sentence called the sentence array. You will recall that the standard scanning practice is to set up a for loop which varies from i equal to 0 to i less than length in increments of 1. This will examine every character stored in the sentence. If that character is a blank I know that one word has ended and another word will start next. All that I do is I increment the number of words by one just put end of line character and continue. At the end I will output the sentence i which is actually the ith character. So, please notice what I am doing. If the sentence is not blank I will come out here and just output that character. If the sentence is blank I will increase the number of words by one and put an end of line character. This will ensure that continuous characters of a word will continue to be printed on the same line, but whenever a word ends a new line character is inserted. So, that the next word will be printed on the next line. This will continue and when this entire for loop ends I would have finished my scanning and I can simply say the given sentence contains so many words. Notice that numbers would actually contain the total number of words counted by this mechanism. Does it work? Well it does print each word on a separate line, but the word count you will find is one less than the actual. This is a very interesting phenomena. Logic of our program appears to be alright, but because the last word did not have a space after it the word count does not get incremented. We should actually check for end of a word by looking either for a blank or for end of string which will be encountered when the last word is finished. This should work. There are some interesting behaviors that you may still notice for different combinations of input. I leave it to you to find any other correction that may be required for this program. In summary we studied in this session how to handle simple strings of character. In the next session we will consider some more problems which require us to handle more complex strings. Thank you.