Tuesday 30 January 2018

Strings in C

Strings in C

            A string is an one dimensional character array which is terminated by a null ( \0 ) character. Each character of the array occupies 1 byte of space in memory.In any programming language, string is a data structure used for text manipulation. Let's see how strings are represented and manipulated in C.

String Representation in C
 
          C language supports string representation with an array of characters terminated by a null character ' \0 '. The null character is a must because it is the only way for compiler to know the end of string. Consider the following statements to initialize a string (character array) :



Please note that the second statement doesn't contain ' \0 ' since in this case, NULL character is automatically appended by the C compiler.

Standard string manipulation function

           The header file < string.h > provides a number of standard functions which can be used for string manipulation. Some of the most commonly used functions are :

 

 There are a lot of standard functions offered by the library.

Following program demonstrates the use of some string manipulation functions :

----------------------------------------------------------------------------------------------------------------------------


 
----------------------------------------------------------------------------------------------------------------------------

Output :
 
Length of str1 is 5
str1 : Hello
str2 : World
str3 : World
Two strings are equal
After concatenating, str1 : HelloWorld

----------------------------------------------------------------------------------------------------------------------------

 Pointer to Strings

          A string is stored in some location in memory. We can use a character pointer to hold the address of that memory location. Precisely, the character pointer stores the address of the first character of the array. Since all the characters are stored in contiguous memory location, we can use the character pointer to access and manipulate the string characters. Following program illustrates the use of pointer to strings.

----------------------------------------------------------------------------------------------------------------------------





----------------------------------------------------------------------------------------------------------------------------

Output :
 
ptr points to string : csegeek
ptr stores the address of : c
ptr now stores the address of : s
ptr now points to string : segeek
sptr points to string : program
ptr now points to string : program

----------------------------------------------------------------------------------------------------------------------------

Two Dimensional Array of Characters

         We can use a 2 dimensional character array to store multiple strings.

Following program illustrates the use of 2 dimensional character arrays. 

----------------------------------------------------------------------------------------------------------------------------





----------------------------------------------------------------------------------------------------------------------------


No comments:

Post a Comment