Unit IV - ARRAY & STRING
+ Prof. S, S.Lamkane
“An array is a collection of elements of the same data type. An array is also called
as subscript variable.”
Declaration of array:
Like any other normal variable, an array has to be declaring before it is used in a
program. The general form of array declaration is:
Datatype Array_name{
Here,
Datatype specifies type of each element of array variable
Size indicates the maximum number of elements that can be stored in an array.
Example:
int {10};
Declares the ‘a’ as array variable of type integer & which is able to store 10 integer
numbers. When array is declared, continuous memory is allocated for the array elements.
For above declaration following is the memory allocation.
a0} aft) af2]___af3]_af4] af5) a[6} a(7] a8] a9]
1000 1002 1004 1006 1008 1010 1012 1014 1016 41018
Fig: Memory Allocation of Array
Assume that the starting address of array is 1000. Every integer requires 2 bytes
memory. So next address is 1002, 1004 & so on.
Initialization of an Array:
After the declaration of array, its element must be initialized. Otherwise, they will
contain ‘garbage’ value. An array can be initialized at either of the following ways.
1, Atcompile time
2. Atrun time.
1. Compile time Initialization:
We can initialize the elements of array in the same way as the ordinary variable
when they are declared. Compile time initialization is also known as Static Binding or
early binding. The general form of initialization of array is:
int a[5]=(2, 5, 3, 4,7}:Example: 1. float total [5]={1.5, 15.70, -10.0};
It will initialize the first three elements to 1.5, 15.70, -10.0 & the remaining two elements to
zero,
Example: 2. int counter[}=f 1, 2,3, 4};
Here, size of array is omitted. In such case the compiler allocates enough space for
initializes elements.
Example: 3. char name[]={J',’0','h’,'n’ , \0"
Declares the name to be an array of five characters, initialized with string “John” & ending
with the mull character back slash zero ‘\0’.
Example: 4. int a[3]=( 10, 20, 30, 40 };
It is illegal in c, because if we have more initialization than the declared size, the compiler
will produce an error.
2, Run time Initialization:
An array can be explicitly initialized at the run time. This type of initialization is
usually applicable in large array. Run time initialization is also known as dynamic binding
or late binding. Consider the following c code which is used to initialize array as run time.
int a[10];
for( i=0; i<10; i++)
{
J
With initialization, array elements with the values entered through the keyboard.
scani(’%d", &afi);
Sample programs:
1. Write a c program to accept ‘n’ numbers. Store this numbers in an array & display
only even numbers.
#include
#include
void main()
{
int no[50},n, i;
clrscr();
printf("\n Enter how many numbei
scanf{"%d", &n);
printf("\n Enter the numbers: ");
for(i=0;icn;i++)
{
scanf(“%d" &no[i]);
printf("\n The even numbers are: "
for(i=0;i
#include
void main()
{
int no, Bin[16], i;
clrscr();
for(i=0;i<16;i++)
{
Bing
printf(“\n Enter the Decimal Number: ");
scanf("%d", &no);
i505
while(no>0)
{
Binfi}=n0%2;
ine
no=no/2;
}
printi(“\n The Binary equivalent is: ");
for(i -)
{
printi(“\n %d’, Binfi]);
getch0);
}
o/P
Enter the Decimal Number: 15
‘The Binary equivalent is : 0000000000001111‘Two Dimensional Array:
Two dimensional array has 2 subscripts. One indicates the row & other indicates the
column. i.e. two dimensional array is used to store elements in the form of table. Tabular
representation of array is also known as matrix.
The general form of two dimensional array is as follows:
Datatype Array_name[row_size][col_size];
For example: int a[3}{2}:
Here, ‘a’ is declared as a two dimensional array having 3 rows & 2 columns. ie, used to
store 6 elements.
Initialization of two dimensional array:
Like a one dimensional array, two dimensional array can be initialized in two ways.
Col0 coll col2
1. Compile time initialization Row0 [ (0,0) (0,1) (0,2)
2. Run time initialization Row1 | (1,0) (1,1) (1,2)
Row2 | (2,0) (2,1) (2,2)
1. Compile time initialization:
There are two different forms of compile time initialization
a. inta[3][2]={
ear
RON
‘
b. intaf3][2}={1,2,5,3,4,2}:
both represented logically as,
Col Cold
RowO 1 2
Rowl = 5 3
Row 2 4 2
Consider the following code:-
int a[3][2);
for(i=0;i<3;i++)
{
for(j=0;j<2;i++)
scanf(“%d", &alil[j]);Here, two for loops are used to initialize two dimensional array. First for loop
represents rows & second for loop represents column.
Memory representation of two dimensional array:
Like a one dimensional array, continuous memory is allocated for two dimensional array as
for above example:
Fig: Memory allocation
Example:
1. write a c program for transpose of a matrix.
(The transpose of a matrix is a matrix with the rows & columns of the original
matrix interchange.)
#includecstdio.h>
#include
void main()
{
int m[S][5], trans[5]I5], x, ¢ i, js
clrscr();
printf("\n How many rows & columns in the matrix: ");
scanf("%d%d", &r, &c};
printi(“\n Enter the array elements: ");
for(i=0;iersi++)
{
for(j=0;j following are the syntax of puts() function.
puts(str);
where str isa string variable containing a string value.
For example: char city[50];
gets(city);
puts(city);
+ Explain different string handling functions with example
String handling function:
Following are the most commonly used string handling functions:
streat(Q); _concatenates two strings
stremp(); compares two strings
strepy(); copy one string to another0
strlen; finds the length of s
strrev); reverse the string1. streat() function:
The strcat() function jobs two strings together. It takes the following form:
streat(string!, string2);
string & string2 are character array when the function strcat is executed string2 is
appended to string].
For example:
Pat! [vy ]elriy \o
Part2 | G | 0 | 0 | D | \o
Execution of statement:
streat(partl, part2);
vie|riy Gj} o]o]p iy
oj|oj]p]|\o
We must make sure that the size of stringl is larger enough to accommodate
the final string
2, stremp() function
‘The stremp() function compares two string.
If they are not equal, it returns numeric difference between the first non-matching
characters in the string, It takes following form,
stremp(string1, string2);
string] & string 2 are string variables or string constants.
For example:
stremp( “their”, “there
will return a value -9 which is the numeric difference between ASCII “i" & ASCII"
stremp("RAM", "RAM");
will return a value 0, Because above strings are equal.
3. strepy() function:
The strepy() function copies one string into another string.
Ittakes the following form:
strepy(string1, sting2);
ing
here, contents of string2 is copied into st
For example:
strepy(city, “phaltan”);
will assign the string “phaltan” to the string variable city.4, strlen() function:
This function counts & returns the number of characters in a string.
It takes following form:
nestrlen(string);
where, n is an integer variable, which stores the value of the length of a string
returned by strlen() function.
5, strrev() function:
This function is used to reverse the string & returns the reversed string.
It takes following form:
trrev(string1);
where, s is a string variable that stores reverse string of string1.
For example:
string1="Phaltan";
s=strrev(stringl);
Now, s contains “natlahp".
Program:
Demonstrate the use of above function.
* si, s2 & s3 are three string variables. Write a program to read two string constant
into s1 & s2 & compare whether they are equal or not? If they are not equal join
them together. Then copy the contents of s1 to the variable s3. Then reverse the
string s1 & check whether it is palindrome or not? Finally print the contents of all
the 3 variables & there lengths.
#include
#include
#includecstring.h>
void main)
{
char s1[20], s2[20}, s3[20];
intx, 11, 12,13;
dlrser();
printi(“\n Enter two strings: ");
scani("%s%s", s1, $2);
xestremp(s1, s2); /* compare string */
iff
Printf("\n String s1 & s2 are equal ");
printf(“\n Strings are not equal ");
o}
strepy(s3, s1);
xestremp(s2, strrev(s2));
if(x-=0)
printi(“\n String s2 is palindrome “);
else
printi(“\n String s2 is not palindrome“);
11 = strlen(s1);
2=strlen(s2);
13=strlen(s3);
printi("\n s1 = %S \t length = 9d", s1, 11);
printf("\n s2 = %S \t length = %d “, s2,12);
printi("\n s3 = %S \t length = %d", s3, 13);
getch();
10