Array and Strings
Array and Strings
com
Declaration of 1D Array
Syntax:
data_type array_name[size];
Eg:
int roll[48];
Initialization of an 1D Array
Compile-time Initialization
Initialize array at declaration.
eg. float salary[5] = {30000.0, 37000.75, 43500.0, 51250.5, 60000};
salary[0] = 30000.0 salary[1] = 37000.75 salary[2] = 43500.0
salary[3] = 51250.5 salary[4] = 60000
Run-time Initialization
Initialize array at run time from user.
float salary[5];
int i;
…
for(i = 0; i < 5; i++){
scanf(“ %f”, &salary[i]);
}
...
Accessing 1D Array
use array elements to display, sort, add elements….
1
Prepared by: Er. Rupesh Shrestha BEL I/I, NPI [email protected]
Examples:
1. Minimum and Maximum among the given elements
2. Write a program to display the third largest number in an array of size N.
3. write a program to input N numbers of integer type in an array and find the sum of square of the
largest number and cube of the smallest number present in the given array.
4. Write a program to read the age of 400 persons and count the number of persons in the age group 70
to 75. Use for and continue statements.
Initialization of 2D Array
I. compile-time
float marks[3][5] = {{50,35,45,49,42}, {49,47,46,48,43}, {49,46,48,47,45}};
marks[0][0] = 50, marks[0][1] = 35, marks[0][2] = 45, … marks[0][4] = 42
marks[1][0] = 49, marks[1][1] = 47, marks[1][2] = 46, … marks[1][4] = 43
marks[2][0] = 49, marks[2][1] = 46, marks[2][2] = 48, … marks[2][4] = 45
ii. Run-time
float marks[3][5];
int i, j;
Accessing of 2D Array
Use nested loop to access elements of 2D array.
Examples:
1. Write a program to multiply given two matrices A and B of order MxN and PxQ.
2. WAP to read a 3x3 matrix. Then multiply each odd number of the matrix by 3 and display the
resultant matrix.
3. Write a program to input p by p matrix and print sum of squares of diagonal elements of matrix.
4. Transpose of matrix
5. Sum of two matrices
2
Prepared by: Er. Rupesh Shrestha BEL I/I, NPI [email protected]
String Handling
Array of characters
Declaration of 1D string:
syntax:
char string_name[char_size];
Example:
char name[30];
char address[50];
char phone[10];
char college[20];
Declaration of 2D string:
syntax:
char string_name[no_of_string][char_size];
Example:
char name[20][30]; //20 names
char address[100][50]; //100 addresses
b. strlwr()
syntax:
strlwr(string);
It finds the lowercase equivalent of given string.
Example:
char name[] = “Narayan”;
strlwr(name);
The name contains narayan.
c. strupr()
syntax:
3
Prepared by: Er. Rupesh Shrestha BEL I/I, NPI [email protected]
strupr(string);
It finds the uppercase equivalent of given string.
Example:
char name[] = “Narayan”;
strupr(name);
The name contains NARAYAN.
d. strrev()
syntax:
strrev(string);
It finds the reverse of the given string.
Example:
char address[] = “Bharatpur”;
strrev(address);
The address contains ruptarahB.
e. strcpy()
syntax:
strcpy(destination string, source string);
It copies content of source string to destination string.
Example:
char name1[30];
char name2[] = “Nepal”;
strcpy(name1, name2);
The name1 and name2 contain Nepal.
f. strcat()
syntax:
strcat(string1, string2);
It concatenates two strings and stores the new string to string1.
Example:
char fname[30] = “Subash ”.
char lname[15] = “Poudel”.
strcat(fname, lname);
The fname stores Subash Poudel.
g. strcmp()
syntax:
strcmp(string1, string2);
It compares two strings character by character and returns
I. 0 if both strings are identical (same).
II. >0 (positive) if string1 is greater than string2.
III. <0 (negative) if string1 is less than string2.
Examples:
strcmp(“Ram”, “Sita”); // returns negative
strcmp(“Sita”, “Ram”); //returns positive
4
Prepared by: Er. Rupesh Shrestha BEL I/I, NPI [email protected]