Unit 3 Introduction Programming
Unit 3 Introduction Programming
Where data type can be any basic or user defined data type.
Array name should follow the rules of an identifier.
SIZE takes an integer value that specifies the number of adjacent cells required for that array.
Example:
int a[10];
Where a is the name of the array, the integer value 10 indicates the size of the array i.e., 10 adjacent
cells are allocated with each cell storing integer element as the data type is int.
The memory is allocated with index starting from zero and ends with its size-1. Pictorially the
memory allocation and its identification is represented as shown below:
20 bytes
References a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
Addresses 1000 1002 1004 1006 1008 1010 1012 1014 1016 1018
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
10 20 30 40 50 60 70 80 90 100
1000 1002 1004 1006 1008 1010 1012 1014 1016 1018
Example:
int threedim[5][10][4];
Two-Dimensional Arrays:
The simplest form of the multidimensional array is the two-dimensional array.
Declaration:
Syntax:
type arrayName [size1][ size2 ];
Where type can be any valid C data type and arrayName will be a valid C identifier.
A two-dimensional array can be think as a table which will have x number of rows and y number of
columns.
Example:
int a[3][4];
0 1 2 3
4 5 6 7
8 9 10 11
Fig. Array after initialization.
2.Using = one at a time.
int a[3][4];
a[0][0]=0;
a[0][1]=1;
a[0][2]=2;
a[0][3]=3;
a[1][0]=4;
a[1][1]=5;
a[1][2]=6;
a[1][3]=7;
a[2][0]=8;
a[2][1]=9;
a[2][2]=10;
a[2][3]=11;
3. Using loops:
int a[3][4];
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
Scanf(“%d”,&a[i][j]);
}
}
Accessing Two-Dimensional Array Elements:
An element in 2-dimensional array is accessed by using the reference, using, row index and column index of
the array. For example:
int val = a[2][3];
The above statement will take 4th element from the 3rd row of the array.
//program on two dimensional array.
#include <stdio.h>
#include <conio.h>
int main( )
{
/* an array with 3 rows and 4 columns*/
int a[3][4] = { {0,1,2,3}, {3,5,6,7}, {8,9,10,11}};
int i, j;
clrscr( );
/* output each array element's value */
printf("The array elements are:\n");
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 4; j++ )
{
printf("%d \t", a[i][j] );
}
printf("\n");
}
getch();
}
Output:
The array elements are:
0 1 2 3
3 5 6 7
8 9 10 11
Introduction to Strings
Strings are sequence of characters enclosed in double quotes. By default a string gets terminated with null
character (\0) marking the end of the string and that character is not visible to the user.
Whereas a character is enclosed in single quote and it is not terminated with any default character.
Example:
printf(“welcome to strings”); // “welcome to strings” is string constant.
Declaration of string variables.
The declaration of string variable is very much similar to declaring a character array.
Syntax:
char stringname[size];
Example:
char name[20];
where name is the name of the string.
Initialization character array:
1. Using string.
Syntax:
char stringname[size]=”value”;
where value can be sequence of characters.
Exmple:
char name[20]=”learn strings”;
From the above declaration, “name” is the name of the string or character array and “learn strings” is the
value initialized to the character array “name” and by default string is terminated with null character(\0)
marking the end of the string. Below we will see how the string is initialized and also see how memory is
allocated.
Name of Name
the array
Index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Value l e a r N s t r i n g s \0
2. Using characters.
Example:
char name[20]={‘l’,’e’,’a’,’r’,’n’,’ ‘,’s’,’t’,’r’,’i’,’n’,’g’,’s’,’\0’};
Reading a string to the character array:
If we suppose that a character array is declared as shown below:
char college_name[25];
then a string can be read using the scanf statement as shown below:
scanf(“%s”,college_name);
or
for(i=0;i<25;i++)
scanf(“%c”,&college_name[i]);
}
Printing a string from the character array:
If we suppose that a character array is declared as shown below:
char college_name[25];
the contents of the character array can be printed as string as shown below:
printf(“%s”,college_name);
or
for(i=0;i<25;i++)
scanf(“%c”, &college_name[i]);
or
for(i=0;i<25;i++)
printf(“%s”,college_name[i]);
Name Function
strcpy(s1, s2) Copies s2 into s1
strcat(s1, s2) Concatenates s2 onto the end of s1
strlen(s1) Returns the length of s1
strcmp(s1,s2) Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2
strchr(s1, ch) Returns a pointer to the first occurrence of ch in s1
strstr(s1, s2) Returns a pointer to the first occurrence of s2 in s1
These functions use the standard header <string.h>. The following program illustrates the use of these string
functions:
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main(void)
{
char s1[80], s2[80];
clrscr();
printf("Enter first string:");
gets(s1);
printf("Enter second string:");
gets(s2);
printf("lengths: %d %d\n", strlen(s1), strlen(s2));
if(!strcmp(s1, s2)) printf("The strings are equal\n");
strcat(s1, s2);
printf ("%s\n", s1);
strcpy(s1, "This is a test.\n");
printf(s1);
if(strchr("hello", 'e')) printf("e is in hello\n");
if(strstr("hi there", "hi")) printf("found hi");
getch();
return 0;
}
Output:
Enter first string:hello
Enter second string:hello
lengths: 5 5
The strings are equal
hellohello
This is a test.
e is in hello
found hi
Array of Strings:
array of strings is 2-D array of characters in which each row is one string.
Example:
char weekdays[7][12]={“Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”,”Sun”};
#include<stdio.h>
#include<conio.h>
int main()
{
// declaring and initializing 2D String
char weekdays[7][12]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
clrscr();
// Dispaying strings
printf("Week days are:\n");
for(int i=0;i<7;i++)
puts(weekdays[i]);
getch();
return 0;
}
Output:
Week days are:
Mon
Tue
Wed
Thu
Fri
Sat
Sun