 Welcome to this session in the course computer programming. In this session, we are going to look at another complex problem of handling strings, which has multiple words in a single sentence. To recapitulate, we have studied the representation of strings using carotide arrays and have seen how strings can be manipulated in C plus clause. In particular in the last session, we discussed how to separate first and last names in a single line. We will now see a generalized approach to solve such a problem. The problem stated again is to read an input sentence, which contains multiple words. These words are separated by one or more blank spaces. Notice that there could be blank spaces even at the beginning or even at the end. Our job is to separate each word, storing it in appropriate arrays. We have to print all words including the count of words. The moot question is that we do not even know how many words are there. So, would it be correct to assign a separate name for each word? The approach to solve this problem is we will read the input sentence in a car array called sentence 200 for example. We expect a sentence to be not more than 199 characters long. We do not know the number of words and therefore, we cannot individually use named array for each word. What we will do instead is we store the words in a two dimensional car array, car word 50 to 100. Notice that this two dimensional array provides for 50 words, actually 49 words. Each row is used to store one word. No, in fact, this array provides for all 50 words, but each word can have at the most 199 characters. Remember, a row will be used to store one word. Each word is a character string and must be terminated by a backslash 0. So, a word can contain up to that is 199 characters match. The approach that we use is the same approach that we did last time. We scan the given sentence one character at a time. Let us look at what happens when we start scanning. Here is a sample sentence. Some blanks, hello, blanks, words, blanks, how, blanks, r, blanks, u, some blanks. This backslash 0 here will actually be inserted by our input operation. The input sentence will end with a backslash n which is the new line character or when you press enter, but this will be replaced by our get s function with a backslash 0. So, therefore, hello world, how are you? A sentence with blanks all are in between and outside will look like this. What do we wish to do with this sentence? We wish to ignore the first three blanks, put hello into consecutive locations for the 0th row of word. So, h, e, l, l, o followed by a backslash 0. We again have to ignore the subsequent blanks till we encounter the second word. This word is word which we wish to put into the first row of word. How will be kept in the second row? R will be kept in the third row, u will be kept in the fourth row. So, 0th, first, second, third, fourth rows will be used because there are exactly five words. At the end, we will come out of the iteration and we should print all the words that we have discovered so far. This gives you some details about how we design the program. I have reproduced the word here, the sentence here again. Please note that this sentence has many words and it has several blanks. Now, I am going to scan this sentence character by character, character by character like this. In the process, whenever I encountered a word of characters which are consecutive non-blank characters, I must put it in one row of the words added. If there are blanks, I need to ignore them. So, when we scan the sentence, let us say using an index i which is initially 0, all that we do is within the iteration ignore blank. If we encounter a blank, simply ignore it, continue scanning. Somewhere, we will come across non-blank characters. When we check for non-blank character, we will have to put it in the current word. So, obviously, the current word, the word count or the character count for that word will all have to be initialized properly. Since we are going to use a two-dimensional array, the current word will be indicated by an index which refers to a different row of the word when the word changes. Within a word, we will increment the character count which is maintained for each word. If a non-blank character is followed by a blank, observe here. O is followed by this blank. Current word ends. Hello as ended. When that happens, we will insert a backslash 0 in the current word. Reset character count to 0 because for the next word, we have to start counting characters again. We will increment the number of words which is stored in a variable numbers and we start the next word. Notice that numbers can also be used as an index for the row of the two-dimensional array word because that represents the number of words. Here is the program which will separate out all words in a sentence. Notice a traditional style commenting slash star followed by slash star. This permits us to write multi-line commands. It says that program which counts the number of words and prints them. It validates all spaces means it ignores all spaces before after and in between the sentence. Finally, it prints all words and the word count. These are merely standard initial declarations of include statements, namespace and integer main function. We define variables and get the input. Variables are sentence which is one single sentence character. The words matrix which is 50 rows by 200 columns in i is the index which we will use for scanning the sentence. Character count is the count within each word. Length numbers, numbers is the number of words and that is set to 0 to begin with. I enter a string. This string is read by using a greatest statement and I now scan the sentence. So, this is the major outer scan through which I propose to assemble all words for i equal to 0. Sentence i not equal to backslash 0. Notice that I need not even bother to find the length of the sentence because I have to scan the complete sentence till the backslash 0 character is encountered. Please note that once I go inside this loop at any point in time, I will be looking at the ith character of the sentence. Let us see what do I do with the ith character. As we outline in our program design, if the ith character is blank, I simply continue with the next iteration which means I ignore the blank. If the ith character is not blank, that means I have encountered a non-blank character which must be inserted in the current word. So, what I do? The ith character of sentence I insert in the character count of current word which is indicated by numbers. I increment the character count by 1 when I finish this. Ordinarily I would have gone back to assemble more characters. However, I check if the next character is a blank or if the next character is a backslash 0. This will indicate the end of this word. Let us go back to the previous slide and examine what we mean. Please note that if this was the non-blank character which I just inserting in the current scan, the next character is a blank. Similarly, the next character here is a blank. Whenever that happens, my current word has ended and that is why what I do now here is that if the I plus 1th element of sentence is a blank or a backslash 0, I terminate the present word which means in the current position which is character count position, I insert a backslash 0. This word has terminated. So, the next word has to start. I increment the number of words, numbers and I reset character count to 0 because in the second word which is the next row of the word matrix, the character count will start from 0, 1, 2, 3, 4 for a new word. That is all I need to do. When I finish this, I will come out here. This is the end of this expression and when I come here, I have finished the entire scan. Notice how beautifully in a single scan of the sentence, I have been able to collect all the words that were there inside the sentence separated by blanks. All that I need to do is I output the number of words which is given by the variable numbers and for each of those words, I simply output words I. Notice that words is the two dimensional array, but C plus plus understands if I just put words I that means the ith row. Since ith row represents a valid string C plus plus permits me to use this as if it was a one dimensional array with the name words. This will output the ith word, I varies from 0 to numbers, I would have got all my words printed. In summary, we can now handle character strings in C plus plus. In real life, however, we need to handle character strings which are part of a large text file. Consider our programs themselves. C plus plus compiler will have to handle these programs which are nothing but lines of text and they contain many symbols not just blank spaces semicolon and or less than, greater than. We need some specific programming techniques to handle such text which is full of all kinds of symbols each one having its own significance. From point of view of computational exercises, consider an example of a data in a spreadsheet which can be saved as a text file in what is popularly known as a csv format. It stands for comma separated values format. We will be including a practice problem which will illustrate such a requirement. We will note, however, that when we want to handle large text, large number of lines containing large number of words, punctuation marks etcetera, we will need to know how to handle files in C plus plus. Thank you so much.