Professional Documents
Culture Documents
Computer Engineering-
Diploma
Unit 4
Array and String
CP
09CE1104
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.
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