You are on page 1of 57

Department of

Computer Engineering-
Diploma
Unit 4
Array and String

CP
09CE1104

Prof. Divyarajsinh Parmar


 Types of array
 Declaration & initialization of 1-D array
 Operation in array
Objectives  String- declaration & initialization
 String functions
 String arrays
 Student Details Program
#include<stdio.h>
void main()
{
long r;
Example int m;
printf(“Enter Roll No and Marks = ”);
scanf(“%ld %d”, &r, &m);
printf(“Roll No: %ld Marks: %d”, r, m);
}
 Now if we want to take, store and display the details
of 5 students…
#include<stdio.h>
void main()
{
long r1, r2, r3, r4, r5;
int m1, m2, m3, m4, m5;
scanf(“%ld %d”, &r1, &m1);
scanf(“%ld %d”, &r2, &m2);
scanf(“%ld %d”, &r3, &m3);
scanf(“%ld %d”, &r4, &m4);
scanf(“%ld %d”, &r5, &m5);

// printf for all 10 variables


}
 But what about the details of 50 or 500 students…
#include<stdio.h>
void main()
{
long r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13,
r14, r15, r16, r17, . . . . .

int m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11,
m12, m13, m14, m15, m16, m17, . . . . .
can we do
this?
Solution Array
 A fixed-size sequenced collection of elements of the
same data type
what is Array?  A variable which can store multiple data of same type
Memory
allocation
Memory
allocation
Memory
allocation
X[0]=14 X[1]=11 X[2]=9 X[3]=34 X[4]=50

 Int x[5];
 X[0]=x[2]+5; x[0]=9+5=14
 X[1]=x[0]-3;
#include<stdio.h>
void main()
{
long r[50];
Student
int m[50];
Details scanf(“%ld %d”, &r[ 0 ], &m[ 0 ] );
Program scanf(“%ld %d”, &r[ 1 ], &m[ 1 ] );
.
.
scanf(“%ld %d”, &r[ 49 ], &m[ 49 ] );
}
#include<stdio.h>
void main()
{
long r[50];
int m[50] , i ;
Student
for( i=0; i<50; i++ ) {
Details scanf(“%ld %d”, &r[ i ], &m[ i ] );
Program }
for( i=0; i<50; i++ ){
printf(“ \n %ld %d”, r[ i ], m[ i ] );
}
}
One
Dimensional
Array
Declaration of
One-
Dimensional
Array
 Array must be initialized before use.

Initialization  Otherwise they will contain “garbage”.

of One-  Two stages of initialization:


Dimensional  At compile Time
Array  At run time
Compile Time
Initialization
Compile Time
Initialization
Compile Time
Initialization
Runtime
Initialization
#include<stdio.h>
void main()
{
int n[10] , largest, i ;
for( i=0; i<=9; i++ ) {
scanf(“%d”, &n[ i ] );
Largest }
number largest = n[0];
for ( i=1; i<=9; i++ ) {
if(largest < n[i])
largest = n[i];
}
printf(“Largest number = %d ”, largest);
}
 Array

Two
Dimensional
Array
 Two-Dimensional Array
Declaration of
Two-
Dimensional
Array
Declaration of
Two-
Dimensional
Array
Declaration of
Two-
Dimensional
Array
 Two stages of initialization (Like One-
Initialization dimensional array) :
of Two-
Dimensional  At compile Time
Array  At run time
Compile Time
Initialization
Runtime
Initialization
#include<stdio.h>
void main()
{
int mat[3][3] , i, j ;
for( i=0; i<=2; i++ ) {
for ( j=0; j<=2; j++ ) {
scanf(“%d”, &mat[ i ] [ j ] );
Matrix }
}
for( i=0; i<=2; i++ ) {
printf (“\n”);
for ( j=0; j<=2; j++ ) {
printf (“ %d ”, mat[ i ] [ j ] );
} }
}
Multi-
Dimensional
Arrays
Note : Most compilers permit seven to ten dimensions.
 Character

String
 A sequence of characters that is treated as a single data
String item.
 Group of characters defined between double quotation
marks
Declaration of
String
variables
Initializing
String
variables
Initializing
String
variables
Reading
String from
Terminal
gets
Output String
Formatted
Output String
 C has several inbuilt functions to operate on string.
There are many important string functions defined in
“string.h” library.
 These functions are known as string handling functions.
string  strlen(s1)
functions  strcmp(s1,s2)
 strcpy(s1,s2)
 strcat(s1,s2)
 strrev(s1)
 strlwr(s1)
 strupr(s1)
 For Example:
 char s1[]=”Their”, s2[]=”There”;
 Returns length of the string.
 l = strlen(s1);
strlen(s1)  it returns 5
 For Example:
 char s1[]=”Their”, s2[]=”There”;
 Compares two strings.
strcmp(s1,s2)  If both strings are equal then its return Zero otherwise any non-zero
value(positive or negitive).
 printf(“%d”, strcmp(s1,s2));
 Output : -9
 For Example:
 char s1[]=”Their”, s2[]=”There”;
 Copies 2nd string to 1st string.
strcpy(s1,s2)
 strcpy(s1,s2) copies the string s2 in to string s1 so s1 is now
“There”.
 s2 remains unchanged
 For Example:
 char s1[]=”Their”, s2[]=”There”;
 Appends 2nd string at the end of 1st string.
strcat(s1,s2)
 strcat(s1,s2); a copy of string s2 is appended at the end of string s1.
Now s1 becomes “TheirThere”
 For Example:
 char s1[]=”Their”, s2[]=”There”;
strrev(s1)  Reverses given string.
 strrev(s1); makes string s1 to “riehT”
 For Example:
 char s1[]=”Their”, s2[]=”There”;
strlwr(s1)  Converts string s1 to lower case.
 printf(“%s”, strlwr(s1));
 Output : their
 For Example:
 char s1[]=”Their”, s2[]=”There”;
strupr(s1)  Converts string s1 to upper case.
 printf(“%s”, strupr(s1));
 Output : THEIR
That’s All

You might also like