C Program Unit3
C Program Unit3
POLYTECHNIC
Gobindapur ,plassey,west Bengal,741156
E-CONTENTS:-C PROGRAMMING
Developed By
Array:-An array is defined as an ordered set of similar data items. The elements of an array are
of same data type and each item can be accessed using the same name. All the data items of an
array are stored in consecutive memory locations
Advantages of Arrays
1.Arrays represent multiple data items of the same type using a single name.
2.In arrays, the elements can be accessed randomly by using the index number.
3.Arrays allocate memory in contiguous memory locations for all its elements. Hence there is
no chance of extra memory being allocated in case of arrays. This avoids memory overflow or
shortage of memory in arrays.
Declaratioin of array :-an array must be declared before it is used. During declaration, the size
of the array has to be specified. The size used during declaration of the array informs the
compiler to allocate and reserve the specified memory locations.
We can initialize the elements of arrays in the same way as the ordinary variables when they
are declared. The general form of initialization of arrays is
1|P ag e
(a) Initializing all specified memory locations:-Arrays can be initialized at the time of declaration
when their initial values are known in advance. Array elements can be initialized with type int,
char etc.
Ex:-int arr[5]={10,15,5,20,30};
During compilation, 5 contiguous memory locations are reserved by the compiler for the
variable arr and all these locations are initialized as shown in figure.
(b) Partial array initialization:-. If the number of values to be initialized is less than the size of
the array, then the elements will be initialized to zero automatically.
Ex:-int arr[5]={10,15};
Even though compiler allocates 5 memory locations, using this declaration statement; the
compiler initializes first two locations with 10 and 15, the next set of memory locations are
automatically initialized to 0's by compiler.
(c) Initialization without size:-Consider the declaration along with the initialization.
In this declaration, eventhough we have not specified exact number of elements to be used in
array b, the array size will be set of the total number of initial values specified. So, the array size
will be set to 8 automatically. The array b is initialized as shown in figure
2|P ag e
(d) Array initialization with a string: -Consider the declaration with string initialization.
Fig: Array Initialized with a String Even though the string "COMPUTER" contains 8 characters,
because it is a string, it always ends with null character(\0). So, the array size is 9 bytes i.e.,
string length 1 byte for null character(\0).
for(i=0;i<100;i=i+1)
{ if(i<50)
sum[i]=0;
else
sum[i]=1; }
The first 50 elements of the array sum are initialized to 0 while the remaining 50 are initialized
to 1 at run time.
#include<stdio.h>
int main() {
scanf("%d", &num);
3|P ag e
printf("\nEnter the values :");
scanf("%d", &arr[i]);
return (0);
#include<stdio.h>
#include<conio.h>
int main()
scanf("%d", &num);
printf("enter elements");
scanf("%d", &arr[i]);
4|P ag e
scanf("%d", &element);
scanf("%d", &location);
num++;
arr[location - 1] = element;
return 0;
getch();
Output of program
Enter no of elements :4
enter elements 10
enter elements 20
enter elements 40
enter elements 50
Enter the element to be inserted 30
5|P ag e
Enter the location 3
10 20 30 40 50
#include<stdio.h>
#include<conio.h>
int main()
scanf("%d", &num);
scanf("%d", &arr[i]);
scanf("%d", &loc);
while (loc<num)
arr[loc - 1] = arr[loc];
loc++;
num--;
6|P ag e
return 0;
getch();
Output of program
Enter no of elements:4
Enter elements: 10
Enter elements: 20
Enter elements: 30
Enter elements: 40
location of element to be deleted: 3
10 20 40
In two dimensional arrays the array is divided into rows and columns. These are well suited to
handle a table of data. In 2-D array we can declare an array as :
Example:-int arr[3][3];
where first index value shows the number of the rows and second index value shows the
number of the columns in the array.
Example: int arr[2][3]={0,0,0,1,1,1}; initializes the elements of the first row to zero and the
second row to one. The initialization is done row by row.
Example:
int a[3][3] = {
{1, 2, 3},
7|P ag e
{4, 5, 6},
{7, 8, 9} };
The above mentioned 2-D array elements can be organized in the following manner
#include<stdio.h>
#include<conio.h>
int main(){
int a[3][3];
int i, j;
for(i=0; i<3; i++)
{
for(j=0;j<3;j++)
{
printf("Enter value for a[%d][%d]:", i, j);
scanf("%d",&a[i][j]);
}
}
8|P ag e
printf("%d\t",a[i][j]);
}
}
getch();
return 0;
}
Output of program
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
scanf("%d",&r);
scanf("%d",&c);
9|P ag e
printf("enter first matrix element\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
mul[i][j]=0;
for(k=0;k<c;k++)
mul[i][j]+=a[i][k]*b[k][j];
10 | P a g e
}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",mul[i][j]);
printf("\n");
getch();
return 0;
Output of program
enter number of row 3
enter number of column 3
enter first matrix element
111
222
333
enter second matrix element
111
222
333
multiply of the matrix
6 6 6
12 12 12
18 18 18
11 | P a g e
3.2.1 declaration and initialization of Strings:-
Declaring Strings:- C does not support string as a data type. It allows us to represent strings as
character arrays. In C, a string variable is any valid C variable name and is always declared as an
array of characters.
Syntax:-char string_name[size]; The size determines the number of characters in the string
name
Initializing strings:- There are several methods to initialize values for string variables
char city[8]={“N”,”E”,”W”,”Y”,”O”,”R”,”K”,”\0”};
The string city size is 8 but it contains 7 characters and one character space is for NULL
terminator(\0)
12 | P a g e
Storing strings in memory:-
A string is stored in array. The character requires only one memory location. If we use one-
character string it requires two locations.
strlen( ) :This function is used to find the length of the string excluding the NULL character. Its
syntax is as follows: Int strlen(string);
int main()
len=strlen(str);
getch();
return 0;
13 | P a g e
}
Output of program
strcpy( ): This function is used to copy one string to the other. Its syntax is as follows
strcpy(string2,string1);
int main() {
char str2[20];
strcpy(str2, str1);
puts(str2);
return 0;
output of program
programming
strcat ( ) This function is used to concatenate two strings. i.e., it appends one string at the end
of the specified string.
int main()
14 | P a g e
strcat(str1, str2);
puts(str1);
return 0;
output of program
This is c program
strcmp ( ) :This function compares two strings character by character (ASCII comparison)
• if the ASCII value of the first unmatched character is less than the second then return
value is negative.
• if the ASCII value of the first unmatched character is greater than the second then
return value is positive.
int main()
int result;
getch();
return 0;
Output of program
Strcmp(str1,str2)=32
N.B.-The ASCII value of 'c' is 99 and the ASCII value of 'C' is 67.hence the return value is 32
15 | P a g e
3.2.3. program to extract substring from string
#include <stdio.h>
#include<string.h>
#include<conio.h>
int main()
printf("Input a string\n");
gets(str);
substr[ch] = str[position+ch-1];
ch++;
substr[ch] = '\0';
return 0;
getch();
Output of program
Enter a string
C programming
Enter position and length of substring
3
7
Required substring is “program”
16 | P a g e
3.2.4. Replacement of string characters
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main()
char str[100],ch,nch;
int i;
gets(str);
ch=getchar();
getchar();
nch=getchar();
for(i=0;i<=strlen(str);i++)
if(str[i]==ch)
str[i]=nch;
printf("\nafter replace:%s",str);
getch();
return 0;
17 | P a g e
}
Output of program
Enter the string : hello
before replace:hello
nafter replace:hexxo
#include <stdio.h>
#include<string.h>
#include<conio.h>
int main()
scanf("%s",str1);
scanf("%s",str2);
str1[i]=str2[j];
18 | P a g e
str1[i]='\0';
printf("\nOutput: %s",str1);
return 0;
getch();
Output of program
Output: helloworld
19 | P a g e