Monday 1 October 2012

ARRAY

Array : An array is a group of related data items that share a common name.  A particular element of the array can  referred of its subscript or index in brackets after the array name.

One Dimensional Array : One dimensional array declaration is as follows

Type variable name[size];

Ex :      float height[50];

Two Dimensional Array : Two dimensional array declaration is as follows

Type array-name[row-size][coloumn-size];

Ex :  int   a[40][90];


String :    A string is array of characters. Declaration of a string is as follows

char string-name[size];

Ex :-  char city[10]

String Handling Functions :  ‘C’ library supports a large number of  string-handling functions that can be used to  carry  out many  of  the string manipulations. The  most  commonly  used string-handling functions

Function                                             Action

Strcat( )                                               Concatenates two strings

Strcmp( )                                             Compares two strings

Strcpy( )                                              Copies one string over anther

Strlen( )                                               Finds the length of string


1. Strcat( ) Function :  The strcat function joins two  strings together. It takes the follwoing form
  strcat(string1, string2);


String1,string2  are character arrays.   When  the function strcat is executed, string2 is appended to  string1.  It  does  so  by removing the null character at  the  end  of string1  and  placing  string2 from  there.   The  string  at string2  remains  unchanged.  We must sure that the  size  of string1 is large enough to accommodate the final string.
 
2. Strcmp( )  function : The  strcmp  function  compares  two strings identified by the arguments and has a value 0 if they are  equal.  If they are not, it has the  numeric  difference between  the first non matching charcters in the  strings.  It takes the form

strcmp(string1, string2)
strcmp(name1, name2);

Ex : strcmp("their","there");

Will  return a value of -9 which is   the  numeric difference between ASCII "i" and ASCII "r". ie, "i" minus "r" in  ASCII code is -9.  If the value is negative , string1  is alphabetically above string2.

3.  Strcpy( )  function : The string function  almost  like  a string-assignment operator. It takes the form
strcpy(string1,string2);

i.e..,  Assigns the contents of string2  to  string1.  String2  may  be  a  character  array  varible  or  a  string constant.

 Ex :- strcpy(city, "delhi");

ie,will  assign  the  string  "delhi"  to  the   string  variable city. similarly the statement

strcpy(city, city2);

Will assigns the contents of the string variable  city2 to  the string variable city1. The size of the  array  city1 should be large enough to receive the contents of city2.

4. Strlen( ) Function :   This  function  counts and returns the  number  of charcters in a string.
n  = strlen(string);

Where n is an integer variable which receives the value of  the length of the string.  The argument may be  a  string constant. The countin ends at the first null character.

Ex :-  n= strlen("DELHI"); then output is n=5.


No comments:

Post a Comment