STRING IN C.

STRING:

STRING LITERALS :

DECLARING STRING VARIABLES:

INITIALIZING STRINGS:

TRAVERSING STRINGS:



PASSING STRING TO FUNCTIONS:

STRING INPUT / OUTPUT:

OUTPUTTING STRING:

THANKS

While working with programs we donot always deal with numeric data, we might sometimes need to operate on character data. For example , we might have programs to sort the list of student names, input the names of items purchased etc. In order to deal with such program we need to study about strings. Before starting with string, let us recall briefly what are character constants and what are values stored in char type variables.A character constant is a single character which might be a alphabet, a digit or a special symbol enclosed in single inverted commas. For example' etc. The character constants have integer values that is determined by the ASCII character set (by most compilers). These character constants str stored in variables of char type . For example : The statement,informs the compiler that ch is a char type variable that holds a character constant 'a'.A string literal (also known as string constant) is a sequence of zero or more characters or symbols that are enclosed in double quotes. C treats it as an array of characters terminated by a special character (\'0') known ascharacter which is inserted automatically by the compiler to mark the end of the string. When a C compiler encounters a string literal, it allocates memory to store the characters of the string plus for one extra character '\0' .Some other valid string literals are :To store a string in a variable , we use an array of characters. It takes the following syntax,Theis actually the name of the character array andrepresents the maximum number of characters that can be stored and it must be a positive integer value. since string are always terminated by a null character so to store a string upto n characters, the size of the array should be. For example , consider the statement,hereis data type ,is name of the arrays of characters andis size specification. The above statement defines an array and reserves 30 bytes for storing characters.Strings are basically arrays that can be initialized at the time of declaration. The following example shows different ways of initializing strings.In the method of initialization, the compiler allocates space for 6 characters for array name. The first five memory locations are filled with' Characters and the last with character '\0' (null character) automatically. In particular, the first characteris stored at name[0], character' is stored at name[1] and so on the last characteris stored at name[5].Now let us consider the following program to explain initialization,An operation in which individual character of a string is handled one by one is known as traversing a string. In order to traverse a string, we use loops and especially for loop as they are relatively easy to handle.The main logic is that as string end with '\0', so we keep on traversing until the character stored at a particular index position is not '\0' . When '\0' is encountered it means that end of string is reached.Since C treats as character arrays so the rules for passing string to function are similiar as that of passing arrays to function.To pass an array of characters(i.e. string) to a function, the array name must be specified without any brackets within the function call . Array are passed by reference so any changes made to the original array in the function are reflected back in the calling function. As string are terminated bu null character, so unlike integer arrays, we generally do not pass the number of elements along with the array name.The following program show how string are passed toa function.C provides a varity of functions for inputting and outputting strings.String can be outputted using library functionandWe have already usedin our programs. Usinglibrary function which is included inheader file, we can display the contents of the string variable.Thefunction takes the following form,Here,is a string variable containing a string literal. It prints the value of the string variable str and then moves the cursor to the beginning of the next line on the screen.Thefunction is used to output the values of the character variables. To print a string of characters usingfunction, we use this function repeatedly to output a string of characters stored in a character array using a loop.We have discussed techniques for inputting string usingandin next page.