Chapter 3: Arrays and Strings: Array
Chapter 3: Arrays and Strings: Array
Chapter 3: Arrays and Strings: Array
PIC-17212
3.2 Declaration and initialization of string variables, string handling functions from standard library
(strlen( ), strcpy( ), strcat( ), strcmp( )).
(08 Marks)
Array:
An array is a collection of similar type elements. These elements are sequentially stored one
after the other in memory. These elements could be all ints, all floats, or all chars etc. Usually, the
array of characters is called a string whereas an array of ints or floats is called simply an array.
Suppose we want to store marks obtained by 100 students and arrange them in ascending order.
There are two options for this:
1) construct 100 variables to store marks of 100 students
2) construct one variable capable of storing all 100 variables.
Obviously, second option is better. But when you will store second value in a simple variable, its
first value is lost. e.g.
int x;
x=10; // current value of x is 10
x=15; // now value of x is 15
In the above example, first we are storing value 10 in variable x. When third statement is
executed, the old value (i.e.10) stored in variable x will be lost and x will now store value 15. In this
situation, arrays can be used to store all the desired values.
PIC-17212
above array, you have to use marks[0] statement, to access second element of array, marks[1]
statement is used and so on. The last element can be accessed by using marks[99] statement.
marks [0] = 50;
name of array
subscript or index
value to store
In the statement,
int marks[100];
array is declared with one subscript, therefore it is called single dimensional array. Subscript of
array starts from zero. Following are some examples of declaring single dimensional arrays:
float salary[25];
char stud_name[30];
double average[10];
or
PIC-17212
arr[1]
arr[2]
10
arr[3]
arr[4]
11
int arr[5];
arr[0]=71;
arr[1]=5;
arr[2]=15;
arr[3]=-97;
arr[4]=1000;
Values can be accepted from user and stored in array e.g.
int arr[3];
printf(Enter value of first element:);
scanf(%d,&arr[0]);
printf(Enter value of second element:);
scanf(%d,&arr[1]);
printf(Enter value of third element:);
scanf(%d,&arr[2]);
//
PIC-17212
Program #1: Write a program in C to initialize and print the contents of an integer array of size 5.
Ans.
#include<stdio.h>
#include<conio.h>
void main( )
{
int marks[ ]={37,59,63,25,71};
int i;
clrscr( );
for(i=0;i<5;i++)
{
printf(\n%d,arr[i]);
}
getch( );
}
Program #2: Write a program in C to declare a float array of size 4.Accept values from user and
store them in array. Print the average of all values.
Ans.
#include<stdio.h>
#include<conio.h>
void main( )
{
float f[4];
float average;
int i;
clrscr( );
printf("Enter the elements of array:\n");
for(i=0;i<4;i++)
{
printf(\nEnter value :);
scanf(%f,&f[i]);
}
average=(f[0]+f[1]+f[2]+f[3])/4;
printf(Average=%f,average);
getch( );
}
OR
PIC-17212
OR
In this program two loops are combined into single loop:
#include<stdio.h>
#include<conio.h>
void main( )
{
float f[4];
float sum=0.0, average;
int i;
clrscr( );
printf("Enter the elements of array:\n");
for(i=0;i<4;i++)
{
printf(\nEnter value :);
scanf(%f,&f[i]);
sum=sum+f[i];
}
average=sum/4;
printf(Average=%f,average);
getch( );
Computer Department, Jamia Polytechnic (0366)
PIC-17212
Program #3: Write a program in C to find the smallest and largest numbers from array.
Ans.
#include<stdio.h>
#include<conio.h>
void main( )
{
int size=0;
int arr[20];
int i;
int smallest, largest;
clrscr( );
printf("Enter the size of array(<20):");
scanf("%d",&size);
printf("Enter the elements of array:\n");
for(i=0;i<size;i++)
{
printf("Enter the element:");
scanf("%d",&arr[i]);
}
smallest=arr[0];
largest=arr[0];
for(i=1;i<size;i++)
{
if(arr[i]<smallest)
smallest=arr[i];
if(arr[i]>largest)
largest=arr[i];
}
printf("%d is smallest.",smallest);
printf("%d is largest.",largest);
getch( );
}
Program #3.1: Write a program in C to search a given number in array.
Ans.
#include<stdio.h>
#include<conio.h>
void main( )
{
Computer Department, Jamia Polytechnic (0366)
PIC-17212
PIC-17212
PIC-17212
}
for(i=0;i<size;i++)
{
for(int j=0;j<size-1;j++)
{
if(arr[j+1]<arr[j])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("Elements of sorted array(ascending order):\n");
for(i=0;i<size;i++)
{
printf("%d\n",arr[i]);
}
getch( );
}
PIC-17212
5) stud[2][0];
6) stud[2][1];
7) stud[3][0];
8) stud[3][1];
10
PIC-17212
Program #6: Write a program in C to initialize a 2x2 array and print its elements.
Ans.
#include<stdio.h>
#include<conio.h>
void main( )
{
int matrix[2][2]={ {1,2},
{7,8} };
clrscr( );
printf(Elements of array\n);
printf(%d,matrix[0][0]);
printf(\t%d,matrix[0][1]);
printf(\n%d,matrix[1][0]);
printf(\t%d,matrix[1][1]);
getch( );
}
Program #7: Write a program in C to add two matrices of size 2x2 and print the result.
Ans.
#include<stdio.h>
#include<conio.h>
void main( )
{
int A[2][2], B[2][2], C[2][2];
int i,j;
clrscr( );
printf(Enter elements of matrix A:\n);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf(%d,&A[i][j]);
}
}
printf(Enter elements of matrix B:\n);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf(%d,&B[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
Computer Department, Jamia Polytechnic (0366)
11
PIC-17212
{
C[i][j]=A[i][j]+B[i][j];
}
}
printf(Elements of matrix C=A+B:\n);
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(%d ,C[i][j]);
}
printf(\n);
}
getch( );
}
Program #8: Write a program in C to find transpose of 3x3 matrix.
Ans.
#include<stdio.h>
#include<conio.h>
int main( )
{
int A[3][3], B[3][3];
int i,j;
clrscr( );
printf("Enter elements of matrix :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("The matrix before transpose:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",A[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
Computer Department, Jamia Polytechnic (0366)
12
PIC-17212
for(j=0;j<3;j++)
{
B[i][j]=A[j][i];
}
}
printf("The matrix after transpose:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",B[i][j]);
}
printf("\n");
}
getch( );
return 0;
}
Program #9: Write a program in C to multiply two matrices of size 3x3 and print the result.
Ans.
#include<stdio.h>
#include<conio.h>
int main( )
{
int A[10][10], B[10][10], C[10][10];
int i,j,k;
clrscr( );
printf(Enter elements of matrix A:\n);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(%d,&A[i][j]);
}
}
printf(Enter elements of matrix B:\n);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf(%d,&B[i][j]);
}
}
for(i=0;i<3;i++)
Computer Department, Jamia Polytechnic (0366)
13
PIC-17212
{
for(j=0;j<3;j++)
{
C[i][j]=0;
for(k=0;k<3;k++)
{
C[i][j]= C[i][j]+A[i][k]*B[k][j];
}
}
}
printf(Elements of matrix C=AxB:\n);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf(%d ,C[i][j]);
}
printf(\n);
}
getch( );
return 0;
}
Program #10: Write a program in C to multiply two matrices of size nxn and print the result.
Ans.
#include<stdio.h>
#include<conio.h>
int main( )
{
int A[10][10], B[10][10], C[10][10];
int i,j,k,r1,c1,r2,c2;
clrscr( );
printf(Enter size of matrix A (rows & columns:\n);
scanf(%d%d,&r1,&c1);
printf(Enter size of matrix B (rows & columns:\n);
scanf(%d%d,&r2,&c2);
if(c1!=r2)
{
printf(Matrix multiplication not possible.);
getch();
return 1;
}
printf(Enter elements of matrix A:\n);
for(i=0;i<r1;i++)
Computer Department, Jamia Polytechnic (0366)
14
PIC-17212
{
for(j=0;j<c1;j++)
{
scanf(%d,&A[i][j]);
}
}
printf(Enter elements of matrix B:\n);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf(%d,&B[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
C[i][j]=0;
for(k=0;k<c1;k++)
{
C[i][j]= C[i][j]+A[i][k]*B[k][j];
}
}
}
printf(Elements of matrix C=AxB:\n);
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf(%d ,C[i][j]);
}
printf(\n);
}
getch( );
return 0;
}
Strings:
String is a collection of characters. In C language character array is called string. When we
write a word or sentence, it is treated as string. A string constant is one dimensional character array
terminated by a null (\0) character.
15
PIC-17212
String declaration:
The general form to declare a string variable is as follows:
char array_name[size];
e.g.
char sname[30];
char address[50];
char dept_name[20];
String Initialization:
Strings can be initialized in two ways as follows:
1) char array_name[size]=string value;
e.g.
char str[20]=Hello World;
2) char
e.g.
char str[20]={H,e,l,l,o, , w,o,r,l,d,\0,};
In the first form, there is no need to use null character in last. C automatically inserts the
null character.
16
PIC-17212
int i;
clrscr( );
printf(%s,str);
getch( );
}
Accepting string from user:
Strings can be read from user in many ways. One character of a string can be read at a time
using scanf( ) or getch( ) functions, or the whole string can be read from user using scanf( )or gets( )
functions.
Program #11: Write a program in C to accept string from user and print it.
Ans.
#include<stdio.h>
#include<conio.h>
void main( )
{
char name[15];
clrscr( );
printf(Enter name : );
scanf(%s,name);
//Note:
printf(\You entered : %s,name);
getch( );
}
17
PIC-17212
gets( ) function:
If string contains spaces then it cannot be fully read by using scanf( ) function. Because when
space is encountered, scanf( ) function assumes that the value after space is for next variable.
Therefore in order to read strings with spaces gets() function can be used. Its general form is:
gets(string_variable);
e.g.
char my_str[20];
gets(my_str);
2) strcpy( ):
18
3) strcmp( ):
4) strcat( ):
PIC-17212
Above functions are present in header file string.h. Therefore if you want to use these
functions string.h must be included in the program.
1) strlen( ):
This function returns length of string i.e. how many characters are there in a string. Its
general form is
strlen(string);
e.g.
int length;
char str[20]=graphics;
length=strlen(str);
After executing above block of code length variable will get a value 8 which is equal to no. of
characters in the string.
2) strcpy( ):
This function copies one string into another. Its general form is:
strcpy(str1,str2);
Here str1 & str2 are two strings. str2 will be copied into str1.
e.g.
char s1[20];
char s2[ ]=microprocessor;
strcpy(s1,s2);
printf(%s, s1);
The output will be microprocessor because s2 is copied in s1.
3) strcmp( ):
This function compares two strings. The comparision is based on ASSCII value of characters.
Its general form is:
strcmp(str1,str2 );
19
PIC-17212
str1 & str2 are two strings. If the two strings are equal, the function will return 0 (zero). If str1 is less
than str2, a value less than zero (i.e. negative value) is returned. If str1 is greater than str2, a value
greater than zero (i.e. positive value) is returned by the function.
4) strcat( ):
This function concatenates or joins two strings. Its general form is:
strcat(str1,str2);
str1 & str2 are two strings. str2 will be joined with str1. The length of str1 must be greater than or
equal to the number of characters present in str1and str2.
Program #14: Write a program in C to print string length using strlen( ) function..
Ans.
#include<stdio.h>
#include<conio.h>
#include<string.h> //include string.h header file
void main( )
{
char arr[25];
int len=0;
clrscr( );
printf(Enter string: );
scanf(%s,arr);
len=strlen(arr);
printf(Length of string is : %d,len);
getch( );
}
Program #15: Write a program in C to copy one string into another by using strcpy( ) function..
Ans.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main( )
{
char s1[20], s2[20];
clrscr( );
printf(Enter string 1: );
gets(s1);
Computer Department, Jamia Polytechnic (0366)
20
PIC-17212
strcpy(s2,s1);
printf(String 2 is : %s,s2);
getch( );
}
21
PIC-17212
22