Unit II Arrays and Strings in C
Unit II Arrays and Strings in C
Arrays - C Programming:
Array is a collection of same data type elements under the same variable identifier
referenced by index number. Arrays allow you to store group of data of a single
type.
ARRAYS:
In the early the values are stored in a variable of primary data types. The
disadvantage is only one value can be stored in a variable at a time. Therefore to
store more than one value, then more than one variable have to be declared. An
additional variable can be declared to store or read two or three values. Imagine
storing the register numbers of students of a college in several variable names to
store the register number of the students. Then the program becomes unreadable
and complex. To simplify that array can be used by just declaring one variables of
type array.
Consider the following example of data type intreg[1000], this means the variable
name „reg‟ can store up to 1000 values. The values can be read, element by
element. Therefore to store a large set of data of same type array is the choice.
Array is a derived data type. It is very effective when working with large number
of data of same data type or different data types.
Definition:
Array is a collection of same data type elements under the same variable identifier
referenced by index number. Arrays allow you to store group of data of a single
type.
Consider the following example:
Intx,y,z;
X=10;y=20;z=30;
The above statements can be rewritten using array form
int x[3];
x[0]=10; x[1]=20; x[2]=30;
When the number of variable is less the statement may not look confusing or
difficult. Now consider the ollowing example.
int y[60];
The array y can store up to 60 values in its elements. The structure and location
value in the array is similar to matrix. Now it is clear that it is not good practice to
declare 60 variables or array y.
Arrays are classified as follows:
1. One dimensional array
2. Two dimensional array
3. Multidimensional array
4. Dynamic array
Strings - C Programming:
String is the collection of characters. It can be represented by 1-d arrays. Header
file used is string.h.
String
String is the collection of characters. It can be represented by 1-d arrays. Header
file used is string.h.
String Declaration:
Datatypevariable_name[size];
Eg: char sar[30];
String Initialization:
Datatype variable_name[size]=string;
Eg: char sar[30]={“Niro”};
Program:
P_1: Initializing 2-D Array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,row1,col1,row2,col2;
clrscr();
printf(“\n Enter array1 size ”);
scanf(“%d%d”,&row1,&col1);
printf(“\n Enter array2 size ”);
scanf(“%d%d”,&row2,&col2);
if(row2!=col1)
{
printf(“Wrong choice entered”);
getch();
exit(0);
}
else
{
printf(“Enter elements”);
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter elements 2”);
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
c[i][j]=0;
for(k=0;k<row2;k++)
{
c[i][j]=c[i][j]+(a[i][j]*b[i][j])
}
}
}
printf(“REQUIRED MATRIX”);
for(i=0;i<row1;i++)
{
for(j=0;j<col2;j++)
{
printf(“\n%d”,c[i][j]);
}
printf(“\n”);
}
getch();
}
}
P_3: Program to demonstrate matrix addition.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j,row1,col1,row2,col2;
clrscr();
printf(“\n Enter array1 size ”);
scanf(“%d%d”,&row1,&col1);
printf(“\n Enter array2 size ”);
scanf(“%d%d”,&row2,&col2);
printf(“Enter the First matrix :”);
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“\n Enter the Second matrix: ”);
for(i=0;i<row2;i++)
{
for(j=0;j<col2;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<row1;i++)
{
for(j=0;j<col1;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf(“\n The Addition of two matrix is \n”);
for(i=0;i<row1;i++)
{
printf(“\n”);
for(j=0;j<col1;j++)
{
printf(“%d\t”,c[i][j]);
}
}
return 0;;
}
P_4: Program to matrix transpose
/*2-D Transpose*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],row,col,i,j;
clrscr();
printf(“\n Enter row size ”);
scanf(“%d”,&row);
printf(“\n Enter column size ”);
scanf(“%d”,&col);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“\n matrix [%d][%d]”,i,j);
scanf(“%d”,&a[i][j]);
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
b[i][j]=a[i][j];
}
}
printf(“\n Transpose of given Matrix: ”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf(“\n%d”,b[i][j]);
}
}
getch();
}
P_5: To search the location of given data in the array / Linear Search
[SEARCHING]
#include<stdio.h>
#include<conio.h>
void main()
{
inti,n,data,a[20],f=0;
printf(“\n Enter the Array Capacity: ”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\n Enter the %d Data: ”,i+1);
scanf(“%d”,&a[i]);
}
}
printf(“\n Enter the data to be searched:”);
scanf(“%d”,&data);
for(i=0;i<n;i++)
{
if(data==a[i])
{
printf(“\n Data is found in %d location”, i+1);
f=1;
}
}
if(f!=1)
{
printf(“\n Data not Found:”);
}
getch();
}
String
String is the collection of characters. It can be represented by 1-d arrays. Header
file used is string.h.
String Declaration:
Datatypevariable_name[size];
Eg: char sar[30];
String Initialization:
Datatype variable_name[size]=string;
Eg: char sar[30]={“Niro”};
1 Built in Sting functions/ String Operation/ Sting Manipulation Function:
There are several string functions to work with string variables and its values.
These functions are available C header file called string.h. Consider the
following exa,ple:
Char string1[15]=”Sriram”;
Char string2[15]=”College”;
Copying String
Strcpy(string1,string2);
This function copy‟s the value of string2 to string1. Now the string1 be
“College”. To add the string2 to string1 the size of the string1 must be sufficient
enough to fit the value of string2. This function will replace the existing value
string1 with string2. Now string1 and string2 are “College”.
String comparison[Case Sensitive]
Strcmp(string1,string2);
This function compares the value from string2 with string1. If both the string1
and string2 are exactly the same then the function will return zero or else it will
return some positive or negative value. For the above example the function will
return negative of positive value. Here string1 and string2 will not change.
Non-Case Sensitive:
strcmpi(string1,string2);
Concatenation Sting
strcat(string1,string2);
Copying String
strcpy(string1,string2);
This function copy‟s the contents of one string into another string.
Find a value in sting
strstr(string1, string2);
This function will find the value of string2 in string1. Assume string1 as
“Apple” and string2 as “Ap” now the function will return position of first
occurrence of “Ap”, since “Ap” is found in “Apple”.
Reversing a string
strrev(string1);
This unction reverse the data o string1 and stores it in string1.
Length of String
Strlen(string1);
This function will return length of the string. For the above example it returns
8.
Convert Uppercase to Lower case
strlwr(string1);
Convert Lower case to Uppercase
strupr(string1);