 Welcome to this session in the course of computer programming. In this session, we shall be discussing some more aspects of handling character strings. Last time, we had seen that the strings can be represented using arrays of type cap. We had also seen that C plus plus provides several useful library functions to handle strings. In fact, we had seen only two functions. In a separate write up accompanying this session, we will also list several other library functions and the processing with these functions achieved. What we propose to do in this session is to discuss some more problems requiring string processing. We start with the statement of two problems. One, we want to read a string which contains first name and last name of a person. There are two names, but these could be separated by one or more blank spaces. What we wish to achieve as a solution to this problem is that we want to separate these two first name and second name and store each part in a different array. So, there should be one array for storing first name another array to store the last name. The second problem is actually an extension of the first one. It says, read an input line containing multiple words separated by one or more blank spaces. Now, separate each word storing it in an appropriate array. Let us first concentrate on the simpler of the two problems. The first problem where we are given a string containing just two names, first name and the last name. Here are some sample names that people might give us when running our program. For example, Nandlal Sarna. Now, this has just one blank in between the first name and second name. Look at the second set, Varsha, blank, blank, blank, opte. So, there are more spaces between the first name and the second name. The third one is still very different. Dasthagi. It has no second name. So, the last line that is shown as a sample input contains only one name. We do not know whether it is first or last, but whatever it is, the sentence has, the line has only one name given. We obviously expect to write a program which will handle all these possible variations in the user input. We design our program by saying that first we will read and store the given line in some character array. Let us call this name string. We will choose the size 60 because we do not expect two parts of a name first name and last name together, including intermediate blanks to be more than 60 characters long. We have to separate out these two parts. So, we define two care arrays, one called first name and the other called last name. We have written the sizes as 60. This is of course excessive, but it guarantees that even if the given line contains a single name and that name is very long, it can still be accommodated in either this or this. Our strategy is simple. We will scan the name string array using an index i. I will start with 0. Keep on looking at each subsequent character in the name string array and we will go on up to the length of the name string. While we scan this array, we will first assemble and print the first name for which we will use an index j. We will search till a non-blank character because the first name will contain a name which will have non-blank characters. If we keep searching for it till the last non-blank character is read, once a blank character is seen, the word has ended. After that, we will skip any following blanks and then assemble and print the last name again reusing the index j. Here is a program. Note that here we have included both the libraries C string and CSTDIO which permits us to use both the functions that we have seen earlier. These are the definitions of the variables in arrays, name string, first name, last name. We have n care which we used to represent the total number of characters in the array name string and we have length f and length l to represent the length of the first name and length of the last name respectively. i and j are index variables which we will use as stated earlier. First we use get us function to read the name string and we calculate n care as simply equal to the string length of name string. We want to start the scan of this name string array, so we start with i equal to 0. We will now assemble and print the first name. Notice the iteration that we set up for this. For j equal to 0, i less than n care, i plus plus j plus plus. So, what we are doing is while assembling the first name, we start with j equal to 0 which will be the index into the first name array. This will be incremented by 1 as i will be incremented. Essentially, what we are doing is we are going to stuff consecutive non-blank characters inside the first name array. Notice that inside this loop, all that we do is if the character that we encounter is not blank, it is part of the current word. So, we insert that into the first name. Notice we pick up the ith character of name string and insert it into the jth character of first name because first name starts with j equal to 0. The moment we encounter a blank character, this if statement will not be valid. I will, this will not be equal to this. I will not execute this, but come to the else part which simply says break. So, this loop iteration is broken. The moment a blank character is encountered which means the first name has ended. We can now put j, the current value of j as the length of first name. Remember, j would have been incremented 1 beyond whatever are the characters that we have assembled. We can simply put at this point the backslash 0 or the terminator character in the first name string and print out the first name string. We have to now continue doing the same thing with the second string, but before that if we have any blank spaces which are consecutive, we need to ignore all of them. So, here is a small loop which keeps consecutive blanks while name string i equal equal blank i plus plus. All that it does is, it simply keeps on incrementing i as long as I keep encountering a blank space. What it means is that when this loop ends, I would have the first non blank character of the second name. Now, if I have already encountered the end of the string name string, please remember the last non blank character would be a backslash 0. If that is so, then I will say that I have reached end of the name string. There are no more names. I have so far collected only one name. So, I will print an exception given string contains only one name and I will return with a flag minus 1. Notice that this code permits me to handle all odd name like Dastagir that we saw in our sample to be properly processed. On the other hand, if I have two parts first name and last name as expected, then the last iteration would have taken care of consecutive blanks. I can now assemble and print the last name. This is done exactly in the same way in which the first name was assembled except that I look for the name string I not equal to blank and insert all non blank character into last name jth position. As usual, if I encounter a blank, I will go out of the loop using the break. I will set length L as j, put a backslash 0 as the last element and print the last name is so many characters and this is the value of the last name. Is it a good approach? Well, yes, we were told that there are two parts of the name and therefore, first name and last name we just assign them for two different arrays. But what if there are multiple parts as in the second problem? You remember second problem says given a sentence containing multiple words separated by each other from one or more spaces get them out into separate arrays or separate strings. Now, it will be very clumsy to keep defining different arrays to hold different words. Why not use a two-dimensional array or a matrix where each row can contain one word and the matrix will have many rows as many as are required to accommodate all words. We shall see this particular approach in another session. In this session, we have studied how to separate two words in a string. Thank you very much.