Documente Academic
Documente Profesional
Documente Cultură
For example, the above said different values can be assigned to the array as
static int numbers [5] = {55, 35, 40, 47, 33);
Now if we assign data as:
static int number[5]={55, 35, 40};
then the different values assigned to different array variables as:
number[0]=55, number[1]=35, number[2]=40, number[3]=0, number[4]=0.
Similarly we can declare and define values in float type of variables.
For example:
static float a[4] = {10. 3, 50, 7.2};
Also you can declare character type data in the array as:
static char name[ ]= { ‘D’, ‘H’, ‘E’, ‘E’, ‘R’, ‘A’, 'J'};
Here data to the character variable be assigned as: name[0] = 'D', name[1] =
'H', name[2] = 'E', name[3] ='E', name[4] = 'R', name[5] = 'A', name[6] = 'J',
name[7] = '\0',
42
(a) Two Dimensional Array
(b) Three Dimensional Array
For example, some of the valid double dimensional arrays are written as below:
int a[10][10];
float b[50][50];
char name[10][20];
Also you can declare two-dimensional array in static form i.e. these type of array
declarations have fixed value or constant value.
The syntax used for static two dimensional array is as follows:
static data-type array-name [row size][column size = {list of value};
For example, suppose you want to assign some fixed value to a variable array
named table by using the static two-dimensional array as.
static int table [2][3] = {15, 7, 12, 2, 20, 8};
char table[2][30]={“kumar”,”dheeraj”};
Syntax:
43
Datatype variable[no_of_dd_arrays][row_size][col_size];
Eg:
int a[2][3][3];
initialization of array
int a[2][3][3] ={
{
{1,2,3},
{4,5,6},
{7,8,9}
},
{
{1,2,3},
{4,5,6},
{7,8,9}
},
};
Int a[2][3][3]= {
{{1,2,3},{4,5,6},{7,8,9} }},
{{1,2,3},{4,5,6},{7,8,9} }},
};
clrscr();
printf(“Matrix is\n”);
44
{
for(k=0; k<3; k++ )
{
printf(“ %d ”,a[i][j][k]);
}
printf(“\n”);
}
printf(“\n\n”);
}
getch();
}
strcat( )
The purpose of this string handling function strcat( ) is to concatenate or combine two
different strings together.
The general syntax used for this is as:
strcat(string1, string2);
Here string1 is combined with string2 and result will be stored (combined
string) in string1. For example, suppose x is "HELLO" and y is "DEAR", then after
applying the strcat( ) function, the result will be "HELLO DEAR" and it will be
stored in the x.
The representation can be done by using the procedure as:
45
strcat (x, y);
printf ("%s", x);
Also we can concatenate more than one string by using nested concatenation:
strcat (strcat (string1, string2), string3);
For example, suppose if x variable has the data "SRI” the y variable has
the data "SAI" and the z variable has the data "RAM", then after applying the
below written nested strcat( ) function:
strcmp( ):
The purpose of this function is to compare two strings. It will check which
string is alphabetically above the others. For comparison ASCII (American
Standard Code for Information Interchange) values are used.
The general syntax is as:
strcmp (string1, string2);
If the ASCII difference of each character in the two different strings from
the first alphabet (till null character of any string occurs first) is zero, then both
the strings are equal. If difference is +ve, then string2 is larger than the string1,
but if the difference is -ve, then string1 is larger than the string2. For example,
suppose you have two character string variables having two different strings i.e.
"their" is stored in the string1 variable and "there" is stored in the string2
variable, then you can compare these two strings by using the strcmp function
as:
char string1[20] , string2[20];
strcmp (string1, string2);
Here the ASCII difference between string1 and string2 is negative and so
the string1 is alphabetically above than the string2. Similarly you can apply
some more operations using this function.
strcpy( ):
The purpose of this function is to copy one string into another string. Note
that target or destination field should be larger than the source field. In other
words size of the string1 should be larger to receive the contents of the string2.
The syntax is as follows:
strcpy (string1, string2);
It will copy the data from the string2 to the string1 variable. Also note
that the length of the string1 should be larger than string2. For example, if you
want to copy the data "BOMBAY" to the string variable city, then the procedure
is as follows:
char city[30];
strcpy(city, "BOMBAY");
46
Note that string1 always should be a character type variable and should
not be a data, whereas you can take string2 as data and string both. For
example,
below statement is an invalid statement:
strcpy("DELHI", "BOMBAY");
strcpy("DELHI", city1);
strcpy(city1,city2);
strcpy(city, "BOMBAY");
Here city 1 and city2 are the two string variable (character type).
strlen( ):
The purpose of this function is to count the number of character in a
string i.e. to find the length of the string. The general syntax is as:
n = strlen (string);
Where n should be of integer type and string variable should be of character
type. For example, suppose if you want to find the number of character (length
of the string) of the string "I am a good boy" assigned to the string variable ct,
then this can be done as
int n;
char ct[20];
n = strlen(ct);
printf ("length is = %d", n);
strrev(st);
printf("\n Reversed string is:%s",st);
Function Description
strcat() Appends a string to another string
strcmp() Compares two strings
strcmpi() Compares two strings ( case insensitive)
strcpy() Copies a string to another string
stricmp() Same as strcmpi()
strlen() Returns the length of a string
strlwr() String converted to lowercase
strncat() n characters of string appended
strncmp() n characters of two strings compared
strncpy() n character of a string are copied to another
strrev() Reverses characters in a string
strstr() Locates one string in another string
strupr() Converts string to uppercase
48