Unit-4 Array & String
Unit-4 Array & String
2
Definition: Array
An array is a fixed size sequential collection of elements of same data
type grouped under single variable name.
3
Declaring an array
Syntax By default array index
data-type variable- starts with 0.
name[size]; If we declare an array of
Integer
size 5 then its index
[0] [1] [2] [3] [4] ranges from 0 to 4.
Array
int mark[5]; First element will be store
at mark[0] and last
integer element will be stored at
mark[4] not mark[5].
[0] [1] [2] [3] [4]
Like integer and float
Float Array
array we can declare
float avg[5]
;
array of type char.
float
4
Initialing and Accessing an Array
Declaring, initializing and accessing single integer
variable
int mark=90; //variable mark is initialized with value 90
printf("%d",mark); //mark value printed
5
Read(Scan) Array Elements
Reading array without
Reading array using loop
loop
1 void main() 1 void main()
2 { 2 {
3 int mark[5]; 3 int mark[5],i;
printf("Enter array element="); for(i=0;i<5;i++)
4 4
scanf("%d",&mark[0]); {
5 printf("Enter array element="); 5 printf("Enter array element=");
6 scanf("%d",&mark[1]); 6 scanf("%d",&mark[i]);
7 printf("Enter array element="); 7 }
8 scanf("%d",&mark[2]); 8 for(i=0;i<5;i++)
9 printf("Enter array element="); 9 {
10 scanf("%d",&mark[3]); 10 printf("%d",mark[i]);
11 printf("Enter array element="); 11 }
scanf("%d",&mark[4]); }
12 12
13 printf("%d",mark[0]);
14 printf("%d",mark[1]);
15 printf("%d",mark[2]);
16 printf("%d",mark[3]); [0] [1] [2] [3] [4]
17 printf("%d",mark[4]);
mark[5] 85 75 65 55 45
18 }
6
Develop a program to count number of positive or
negative number from an array of 10 numbers.
Program
1 void main(){ Outpu
2 int num[10],i,pos,neg; t
3 pos = 0; Enter array element=1
4 neg = 0; Enter array element=2
5 for(i=0;i<10;i++) Enter array element=3
6 { Enter array element=4
7 printf("Enter array element="); Enter array element=5
8 scanf("%d",&num[i]); Enter array element=-1
9 } Enter array element=-2
10 for(i=0;i<10;i++) Enter array element=3
11 { Enter array element=4
12 if(num[i]>0) Enter array element=5
13 pos=pos+1; Positive=8,Negative=2
14 else
15 neg=neg+1;
16 }
17 printf("Positive=%d,Negative=
18 %d",pos,neg);
}
7
Develop a program to read n numbers in an array and
print them in reverse order.
Program
1 void main() Outpu
2 { t
3 int num[100],n,i; Enter number of array
4 printf("Enter number of array elements= elements=5
5 "); Enter array element=1
6 scanf("%d",&n); Enter array element=2
7 //loop will scan n elements only Enter array element=3
8 for(i=0;i<n;i++) Enter array element=4
9 { Enter array element=5
10 printf("Enter array element="); 5
11 scanf("%d",&num[i]); 4
12 } 3
13 // 2
14 negative loop to print array in reverse ord 1
15 er
16 for(i=n-1;i>=0;i--)
17 {
printf("%d\n",num[i]);
}
} 8
Practice Programs
1) Develop a program to calculate sum of n array elements in C.
2) Develop a program to calculate average of n array elements in C.
3) Develop a program to find largest array element in C.
4) Develop a program to print sum of second and second last element of
an array.
5) Develop a program to copy array elements to another array.
6) Develop a program to count odd and even elements of an array.
9
Multi Dimensional Array
Declaring 2 Dimensional Array
Syntax A two dimensional array
data-type variable-name[x][y]; can be seen as a table
with ‘x’ rows and ‘y’
Declaratio columns.
n
int data[3][3]; // The row number ranges
This array can hold 9 elements from 0 to (x-1) and
column number ranges
int data[3][3]; from 0 to (y-1).
Column-0 Column-1 Column-2
data[0] data[0] data[0]
Row-0
[0] [1] [2]
data[1] data[1] data[1]
Row-1
[0] [1] [2]
data[2] data[2] data[2]
Row-2
[0] [1] [2] 11
Initialing and Accessing a 2D Array: Example-1
Program
1 int data[3][3] = {
2 {1,2,3}, //row 0 with 3 elements
3 {4,5,6}, //row 1 with 3 elements
4 {7,8,9} //row 2 with 3 elements
5 }; Column-0 Column-1 Column-2
6 printf("%d",data[0][0]); //1
7 printf("%d",data[0][1]); //2 Row-0 1 2 3
8 printf("%d\n",data[0][2]); //3
9 Row-1 4 5 6
10 printf("%d",data[1][0]); //4
11 printf("%d",data[1][1]); //5 Row-2 7 8 9
12 printf("%d\n",data[1][2]); //6
13
14 printf("%d",data[2][0]);//7
15 printf("%d",data[2][1]); //8
16 printf("%d",data[2][2]); //9
1 // data[3][3]
2 can be initialized like this also
int data[3][3]={{1,2,3},{4,5,6},{7,8,9}}; 12
Initialing and Accessing a 2D Array: Example-2
Program
1 int data[2][4] = {
2 {1,2,3,4}, //row 0 with 4 elements
3 {5,6,7,8}, //row 1 with 4 elements
4 };
5 printf("%d",data[0][0]); //1
6 printf("%d",data[0][1]); //2 Col-0 Col-1 Col-2 Col-3
7 printf("%d",data[0][2]); //3
8 printf("%d\n",data[0][3]); //4 Row-0 1 2 3 4
9
10 printf("%d",data[1][0]); //5 Row-1 5 6 7 8
11 printf("%d",data[1][1]); //6
12 printf("%d",data[1][2]); //7
13 printf("%d",data[1][3]); //8
1 // data[2][4]
2 can be initialized like this also
int data[2][4]={{1,2,3,4},{5,6,7,8}};
13
Read(Scan) 2D Array Elements
Program
1 void main(){
2 int data[3][3],i,j;
3 for(i=0;i<3;i++)
4 {
5 for(j=0;j<3;j++)
Outpu
6 { t
7 printf("Enter array element="); Enter array element=1
8 scanf("%d",&data[i][j]); Enter array element=2
9 } Enter array element=3
10 } Enter array element=4
11 for(i=0;i<3;i++) Enter array element=5
12 { Enter array element=6
13 for(j=0;j<3;j++) Enter array element=7
14 { Enter array element=8
15 printf("%d",data[i][j]); Enter array element=9
16 } 123
17 printf("\n"); 456
18 } 789
19 }
14
Develop a program to count number of positive, negative
and zero elements from 3 X 3 matrix
Program
1 void main(){ Outpu
2 int data[3][3],i,j,pos=0,neg=0,zero=0; t
3 for(i=0;i<3;i++) Enter array element=9
4 { Enter array element=5
5 for(j=0;j<3;j++) Enter array element=6
6 { Enter array element=-3
7 printf("Enter array element="); Enter array element=-7
8 scanf("%d",&data[i][j]); Enter array element=0
9 if(data[i][j]>0) Enter array element=11
10 pos=pos+1; Enter array element=13
11 else if(data[i][j]<0) Enter array element=8
12 neg=neg+1; positive=6,negative=2,zero
13 else =1
14 zero=zero+1;
15 }
16 }
17 printf("positive=%d,negative=%d,zero=
18 %d",pos,neg,zero);
}
15
Practice Programs
1. Develop a program to perform addition of two matrix.
2. Develop a program to perform multiplication of two matrix.
16
String
(Character Array)
Definition: String
A String is a one-dimensional array of characters terminated by a
null('\0').
[0] [1] [2] … [9]
char name[10]
;
Each character in the array occupies one byte of memory, and the last
character must always be null('\0').
The termination character ('\0') is important in a string to identify
where the string ends.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
name[10 D A R S H A N \0
]
18
Declaring & Initializing String
Declaratio
n
char name[10]
;
Initialization method
1:
char name[10]={'D','A','R','S','H','A','N','
\0'};
Initialization method
2:
char name[10]="DARSHAN";
//'\
0' will be automatically inserted at the end in this type of declarati
on.
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
name[10 D A R S H A N \0
]
19
Read String: scanf()
Program Outpu
1 void main() t
2 { Enter name: Darshan
3 char name[10]; Name=Darshan
4 printf("Enter name:"); Outpu
5 scanf("%s",name); t
Enter name: CE Darshan
6 printf("Name=%s",name);
Name=CE
7 }
20
Read String: gets()
Program
Outpu
1 #include<stdio.h> t
2 void main() Enter name:Manav Rachana
3 { Name= Manav Rachana
4 char name[20];
5 printf("Enter name:");
6 gets(name);
7 //read string including white spaces
8 printf("Name=%s",name);
}
gets(): Reads characters from the standard input and stores them as a string.
puts(): Prints characters from the standard.
scanf(): Reads input until it encounters whitespace, newline or End Of
File(EOF) whereas gets() reads input until it encounters newline or End Of
File(EOF).
gets(): Does not stop reading input when it encounters whitespace instead it
takes whitespace as a string.
21
String Handling Functions : strlen()
C has several inbuilt functions to operate on string. These functions are
known as string handling functions.
strlen(s1): returns length of a string in integer
Program
Outpu
1 #include <stdio.h> t
2 #include <string.h> //header file for string functions Enter string: CE
3 void main() Darshan
4 { 10
5 char s1[10];
6 printf("Enter string:");
7 gets(s1);
8 printf("%d",strlen(s1)); // returns length of s1 in in
9 teger
}
22
String Handling Functions: strcmp()
strcmp(s1,s2): Returns 0 if s1 and s2 are the same.
Returns less than 0 if s1<s2.
Returns greater than 0 if s1>s2.
Program
Outpu
1 void main() t
2 { Enter string-
3 char s1[10],s2[10]; 1:Computer
4 printf("Enter string-1:"); Enter string-
5 gets(s1); 2:Computer
Outpu
6 printf("Enter string-2:"); Strings
t are same
7 gets(s2); Enter string-
8 if(strcmp(s1,s2)==0) 1:Computer
9 printf("Strings are same"); Enter string-
10 else 2:Computer
11 printf("Strings are not same"); Strings are same
12 }
23
String Handling Functions
For examples consider: char s1[]="Their",s2[]="There";
Syntax Description
strcpy(s1,s2) Copies 2nd string to 1st string.
strcpy(s1,s2) copies the string s2 in to string s1 so s1 is now
“There”. s2 remains unchanged.
24
String Handling Functions (Cont…)
For examples consider: char s1[]="Their",s2[]="There";
Syntax Description
strrev(s1) Reverses given string.
strrev(s1); makes string s1 to “riehT”
strlwr(s1) Converts string s1 to lower case.
printf("%s",strlwr(s1));
Output : their
Converts string s1 to upper case.
strupr(s1)
printf("%s",strupr(s1));
Output : THEIR
Copies first n character of string s2 to string s1
strncpy(s1,s2,
n) s1=""; s2="There";
strncpy(s1,s2,2);
printf("%s",s1);
Output
Appends: Th
first n character of string s2 at the end of string s1.
strncat(s1,s2,
n) strncat(s1,s2,2);
printf("%s", s1); Output :
TheirTh 25
String Handling Functions (Cont…)
For examples consider: char s1[]="Their",s2[]="There";
Syntax Description
strncmp(s1,s2, Compares first n character of string s1 and s2 and returns similar
n) result as strcmp() function.
printf("%d",strcmp(s1,s2,3));
Output
Returns: the
0 last occurrence of a given character in a string s1.
strrchr(s1,c)
printf("%s",strrchr(s2,'e'));
Output : ere
26
Thank you