UNIT 3 Arrays Strings
UNIT 3 Arrays Strings
UNIT – 3
ARRAYS AND STRINGS
ARRAYS
CONCEPTS
“An array is defined as fixed size sequenced collection of
elements of same data type”
“An array is defined as collection of elements that share a
common name”
“An array is defined as collection of homogeneous elements”
“An array is defined as collection of data items stored in
contiguous memory locations”
ARRAY PROPERTIES
The type of an array is the data type of its elements
The location of an array is location of its first element
The length of an array is the number of data elements in the array
The size of an array is the length of the array times the size of an
element.
marks[0]
marks[1]
marks[2]
marks[3]
marks[4]
marks [0]=30;
marks [1]=45;
marks [2]=55;
marks [3]=10;
marks [4]=73;
marks[0] 30
marks[1] 45
marks[2] 55
marks[3] 10
marks[4] 73
Here the variable “i” is used in the subscript for referring various
elements of the array. The for loop causes the process to be repeated for 10
times in asking to enter the students marks from user. For the first time the
value of “i” is 0 read through scanf() statement and get the value of marks[0]
which is the first element of the array. The process is repeated until “i” value
terminates the condition.
the first element of the array. The process is repeated until “i” value
terminates the condition.
Initialization of Arrays
After declaring an array the individual elements of an array are
initialized otherwise they will contain garbage values.
The initialization takes place in two ways.
Compile time initialization
Run time initialization
char name[10] = {‘c’, ’o’, ’m’, ’p’, ’t’, ’e’, ’r’, ’s’, ‘\0’};
From the above example the first 50 elements of the array are
initialized to 0 and second 50 elements of the array are initialized to 1
at run time.
We can also use read function called scanf() to initialize an array.
int x[3];
scanf(“%d%d%d”, &x[0],&x[1],&x[2]);
return 0;
}
{
int a[2][2],b[2][2],c[2][2],n,m,i,j;
clrscr();
printf("enter the size of the matrix");
scanf("%d%d", &n, &m);
printf("enter the a matrix elements");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
}
printf("enter the b matrix elements");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
scanf("%d",&b[i][j]);
}
printf("the matrix addition is ");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
c[i][j]=a[i][j]+b[i][j];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d",c[i][j]);
}
return 0;
}
Program to perform matrix subtraction
#include<stdio.h>
#include<conio.h>
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
10
INTRODUCTION TO PROGRAMMING
int main(void)
{
int a[2][2],b[2][2],c[2][2],n,m,i,j;
clrscr();
printf("enter the size of the matrix");
scanf("%d%d", &n, &m);
printf("enter the a matrix elements");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
}
printf("enter the b matrix elements");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
scanf("%d",&b[i][j]);
}
printf("the matrix addition is ");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
c[i][j]=a[i][j]-b[i][j];
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d",c[i][j]);
}
return 0;
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d\t",c[i][j]);
}
return 0;
}
Program to perform matrix transpose
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[10][10], t[10][10], i, j, n, m;
clrscr();
printf("enter the size of the matrix");
scanf("%d%d", &n, &m);
printf("enter the matrix elements");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d:", &a[i][j]);
}
printf("\n");
}
printf("the transpose of matrix is:");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
t[j][i]=a[i][j];
}
printf("\n");
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
13
INTRODUCTION TO PROGRAMMING
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t", t[i][j]);
}
printf("\n");
}
return 0;
}
STRINGS
STRINGS CONCEPTS
A string is a series of characters treated as a single unit. The string is
classified into two categories.
Fixed length strings
Variable length strings
Delimited Strings
To identify the end of the string is a delimiter at the end of the
delimited strings. The delimiter is specified by null character(\0).
C STRINGS
“A string is defined as sequence of characters that is treated as
single data item”.
“A string is variable length array of characters that is delimited
by the null character”.
Examples abcd
Ab123cde
Ramu etc
Storing Strings
String is stored as array of characters. It is terminated by null
character (‘\0’).
As string is stored as an array, the name of the string is a pointer to
the beginning of the string.
The diagram to show how a string is stored in memory is shown
below:
String Delimiter
Why do we need null character at the end of the string? The answer is
string is not a type but it is data structure.
The physical structure is the array and the logical structure is its
definition.
We need to identify the logical end of the string within the physical
structure.
If data is variable length, then we need to determine the end of the
data. Therefore we use null character to determine the end of the
string.
The difference between array and string is shown below:
String Literals
“A group of characters defined between double quotation marks is
called as string constant”
Examples “abcd”
“Ab123cde”
“Ramu” etc
Initialization of strings
There are two types of initializations. They are as shown below:
char str[9]= “Good Day”;
char city[9]={‘G’, ’o’, ’o’, ’d’, ‘ ‘, ’D’, ’a’, ’y’, ’\0’};
Line to string
The gets and fgets functions take a line terminated by a newline from
the input stream and make a null terminated string.
It is called line-to-string input functions
The syntax is as follows
char * gets(char * strptr);
char * fgets(char * strptr, int size, FILE *sp);
The diagrammatic representation about how they work is shown
below.
String to line
The puts / fputs functions take null terminated string from memory
and write it to a file.
It is called as string –to-line output functions.
The syntax is as follows
int puts(const char * strptr);
int fputs(const char * strptr, FILE *sp);
The diagrammatic representation about how they work is shown
below.
strcat() function
The strcat function is used to join two strings together. It takes the
following form:
strcat(string1, string2);
Here the string1 and string2 are character arrays. When the function
strcat is executed, the string2 is appended to string1 and string1 size
should be large enough to hold the entire string after concatenation. It does
by removing the null character at the end of the first string and placing the
string2 from there. The string2 remains unchanged.
For example
String1=”VERY” V E R Y
String2=”GOOD” G O O D
strcat(string1,string2)
Result is string1=”VERY GOOD” V E R Y G O O D
String2=”GOOD” G O O D
strcmp() function
The strcmp function is used to compare two strings. It is done by
character by character. If the value is 0 then they are equal. It they are not
equal then we should consider the numeric difference between them. It
takes the following form:
strcmp(string1, string2);
Here the string1 and string2 are string variables or string constants.
For example
strcmp(name1,name2);
strcmp(name1,”john”);
strcmp(“rom”,”ram”);
if(x==0)
printf(“the strings are equal”);
else
printf(“the string are not equal”);
return 0;
}
strcpy() function
The strcpy function is used to copy one string over another string. It
takes the following form:
strcpy(string1, string2);
here the contents of string2 are assigned to string1 where string2 may be a
string constant. For example
strcpy(city,”delhi”);
#include<string.h>
#include<conio.h>
int main(void)
{
char s1[100], s2[100], i;
clrscr();
printf(“Enter the string”);
gets(s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
puts(s2);
return 0;
}
strlen() function
The strlen function counts and returns the number of characters in a
string. It takes the following form:
n=strlen(string);
here “n” is integer variable, which receives the integer value of length of the
string. The parameter is a string constant. For Example
n=strlen(“good”);
char str1[10];
clrscr();
printf(“Enter the string”);
gets(str1);
x=strlen(str1);
printf(“ the length of the string is %d”,x);
return 0;
}
strrev() function
The strrev() function is used to find the reverse of the given string. It
takes the following form:
strrev(string);
printf(“%c”,str1[i]);
return 0;
}