 Dear learner, myself Tapasi Kashyap Das, Assistant Professor in Computer Science, Krishna Kanta Handik, State of our University. Today, I am going to discuss the concept of strings, which is from the course Computer Programming Using C-Lingues. In my previous discussion, I have discussed how arrays can be used in programming. I have explained one-dimensional array as well as two-dimensional arrays in my previous class. Today, in this session, I will talk about strings in C-Programming Language. I will discuss how strings can be declared, how initialization of strings can be done, entering values in a string and accessing values in string. Now, what is a string? As we have already learned that an array is a collection of homogeneous elements that are finite and ordered. Homogeneous means same type of data. Arrays can be integer array, float array, character array, etc. Now, an array of character is called a string. That is, strings are also arrays but of character data type. Every string contains one or more characters that comprises of the string followed by a null character that indicates the end of the string. Arrays can be multidimensional but strings are always one-dimensional array of characters and which are always terminated by a null character. Null character is represented by slash 0. Now, how can we declare a string? A string can be declared in the same way as an array of any data types. We can declare a string by specifying the data type as character followed by the name of the string and the size of the string. This can be written as, here the size of the string should be the sum of the size of the number of characters that the string can contain plus the size of the size for the null character that designates the end of the string. Here I have shown some examples of string declarations. So to hold the null character at the end of the string, the size of the string should be one more than the number of characters intended to enter in the string. This is a string declaration statement. In a string declaration statement, the data type will be a character then it is followed by the name of the string, then the size of the string and one semicolon is required and this third bracket is required to specify the size. This is a one single string declaration statement. Let us write some examples of string declarations statement. Character name, size is suppose 15. This is one string declaration statement. Again another A, U, T, H, R. The string name is author. Suppose the author name can be Landy. So I have taken 20. This is one string declaration statement. Another string declaration statement suppose book, then suppose 30. These three are some string declaration statement. It will contain 15 characters including the null character. It will contain name of authors of maximum 20 characters length and it will contain book name of maximum 30 characters length. These are string declaration statement. Let us declare a string to store name of a person. Here I am declaring a string of size 15. Here name is the string name. 15 is the size of the character array where we can store maximum of 15 characters including the null character. In this diagram a name of a person Riku, R-I-K-U is stored in the string. There are only four characters. But one more space is required to store the null character at the end. So the array should be minimum of size 5. If the array size is 4 then only three characters that is up to R-I-K Rik and a null character will be stored in the declared array. In this diagram a string element is identified with the help of the index or subscript value. The starting index value in the string is 0. The first address is known as the base address. Here in this example base address is 100. Next consecutive memory addresses are 101, 102, 103 and 104. In this example the first element of the string is stored in the memory address 100. For most C compilers character variable requires 1 byte of storage. Since strings are stored in contiguous memory location so the second element will be stored next to the first element in location 101 and so on. Now I am coming to how to initialize a string. We can initialize the elements of the string at the same time when we declare the string. Initialization is generally done when we already know the character we lose of the string. Just like declaration in initialization also we give the data type of the string followed by the name and the size of the string. But in addition to this we also provide the values of the character elements of the string within the second bracket separated by commas. Let us write one example where the string is initialized at the time of declaration. Name of string at the time of declaration character, suppose the string name is str, size is 6, this semicolon is required at the end and this is the null character. Although the name is, this letter is hello is of length 5, there are 5 characters but one more character is required for storing the null character. So the size is 6 here, this is the size and this is the name of the string. This statement is initialization at the time of declaration. We can also write the initialization statement without mentioning the size, that is without specifying the size. In this initialization statement I have not mentioned the size here and I have specified individual characters within this single code. This statement can also be written as this is also an initialization at the time of declaration, here also I have not mentioned the size. Here I have used the double codes instead of single code because I have, at the time I have written the whole word. I can write the whole word or text within this second bracket but in such cases I have to use this double code instead of this single code. These are some initialization statements. There is another such simpler way to initialize the string. We can also give the values of the character elements together in double codes instead of providing them individually. For example we can write character str third bracket equal to within double code hello, then one semicolon. In this case the compiler assumes the string size to be equal to the number of character values provided inside the braces plus one more for the null character. Now I am coming to how elements can be entered in a string. We can insert values into a string at the time of initialization but apart from that we can also insert values into the string by accessing them individually. For example if we have a string name of six elements we can insert the values for each of the six string positions as follows. Let us now suppose that we have a string of 50 characters which will contain name of a book. A solution to this problem is to use loop control structure statements like while, do while and for loop. Let us look at a code where with the help of the loop we can easily enter any number of the character values. Here a for loop is written in this for loop i is initialized to zero then i less than 50 then i plus plus and within the second bracket one input statement is written scan f percent c comma and percent name of i. This for loop is written for entering values character by character into the string. The string name is name, ampersand name of i gives the address of the ith element of the string where the current character data values need to be stored. An easier solution to this is that instead of providing the data values individually we can provide them together as a string. For this purpose we have to use percent s instead of percent c. Let us look at the statement. Here in this statement the scanf statement accepts a string instead of a single character and stores it in the address of the given string location. Now how can we access values from a string? Once the values have been inserted we may need to access the string for various purposes. The procedure for accessing the values of a string is similar to entering the values. This can be implemented in two ways. In the first way we access the values for string individually as follows. In the code a for loop is used to traverse from the first element of the string to the last element. The printer statement displays the character value which is stored in the address of the given string location. The array location is moved from the first element position to the last element position using the loop. In the second way we can access the values of the string together. This can be written as printf person s comma name. Let us now discuss a complete C program for entering and accessing elements in the string. Here a complete C program is written. In the first two lines two header files are included. First header file is written for inputting the standard input and output statement in the second header file and that is conio.h is used for clear screen and get chase function. In the main function two character array that is strings are declared string1 and string2 which is of size 10. Then one integer variable is declared after that clear screen function is declared. Then entering characters individually using loop is shown. One printf statement is written for entering the first string elements in the string string1. If for loop is used the for loop is from 0 to 9 and one scanf statement inside the for loop is written and inside the scanf statement 1 by 1 characters are entered into the string1. So it is written person s c inside the scanf statement. In the printf statement next printf statement enter the second string into the string2. Then once scanf statement is written after entering two string then the program will display the entered string. Two printf statement is written for displaying the first string and the second string. And output of this program is shown here. Enter the first string in string1 suppose I have entered the string gohati and in the second string I have entered the string that is word asam. Enter entering the first and second string the both the strings are displayed using two printf statement. It is the first string is gohati and the second string is asam. This is a simple example for entering and displaying elements into a string using person c and person s in the scanf statement. In the next part I will mainly discuss various string handling function I will discuss some program where string manipulation is accomplished without using string library function. In my next class I will discuss various string handling functions and I will discuss some programs where string manipulation is accomplished with or without using string library functions. Thank you. You