You are on page 1of 27

ARRAYS AND STRINGS

Arrays
An array is a collection of similar data items, that are stored under a
common name.
A value in an array is identified by index or subscript enclosed in square
brackets with array number.
For example q[n] represents the array q of n elements.
The array index value starts from 0. So q[0] refers to the first element of the
array.
Arrays can be classified into

One-Dimensional

Two-Dimensional

Multi-Dimensional
Characteristics of array
Instead of declaring n variables for n values, only one variable is
enough using array
Ex: int a[3];
All the elements of an array share the same name
Element number in an array plays major role for accessing each
element
Ex: int a[3]={1,2,3};
a[0]=1 a[1]=2 a[2]=3
Any particular element of an array can be modified separately
without disturbing other element
Ex: int a[3]={1,2,3};
for replace 2 by 10 in above statement,
write statement a[1]=10;
Any array element a[] can be assigned to another ordinary
variable or array variable of its type
Ex: b=a[2];
a[1]=a[2];
One Dimensional Arrays
The collection of data items can be stored under one variable name using
only one subscript, such an variable is called one-dimensional array.

Declaration of Array:

The general form of declaration is


data_type variable_name[size];
Where,
data_type specifies the data type of the array variable
size specifies the maximum number of elements that can be stored inside
the array.
Example:

float height[7];
One Dimensional Arrays
In memory seven storage locations are reserved for the
variable height as shown below:
height
10.5 height[0
]
height[1
]
height[2
]
height[3
]
height[4
] into an element in the array simply by
A value can be stored
specifying the statement
height[5
]
height[0] = 10.5;
height[6
the value 10.5 is stored as the first element of the array height.
]
One Dimensional Arrays
Initializing the Arrays:
The general form is
data_type array_name[size] = {list of values separated by comma};

Example:
int num[4] = {1, 2, 3, 4};

The above statement will declare an integer array of size 4 and will assign value
as num[0] = 1, num[1] = 2, num[2] = 3, num[3] = 4.

We can also declare the above statement as


int num[ ] = {1, 2, 3, 4};
This approach works fine as long as we initialize every element in the array.
One Dimensional Arrays
//No. of odd and even nos. in an array
#include<stdio.h>
void main()
{
int num[50], odd=0,even=0,n,i; Output
printf(Enter the size of an array:);
scanf(%d, &n); Enter the size of an array:5
printf(Enter the numbers\n);
Enter the numbers
for(i=0;i<n;i++)
{ 12345
scanf(%d, &num[i]); List contains
if((num[i] % 2) == 0) 3 odd numbers
even++; 2 even numbers
else
odd++;
}
printf(List contains\n);
printf(%d odd numbers \n%d even numbers,odd,even);
}
One Dimensional Arrays
//search specified element in an array
#include<stdio.h>
void main()
{
int num[10],i,n; Output
printf(Enter 10 elements\n);
for(i=0;i<10;i++)
scanf(%d, &num[i]); Enter 10 elements
printf(Enter element to search:); 1 2 3 4 5 6 7 8 9 10
scanf(%d, &n); Enter element to search:3
for(i=0;i<10;i++) Element found
{
if((num[i] == n)
break;
} Enter 10 elements
if(i<10) 1 2 3 4 5 6 7 8 9 10
printf(Element found); Enter element to search:13
else
Element not found
printf(Element not found);
}
Two Dimensional Arrays
Often there is a need to store and manipulate two dimensional data structure
such as matrices & tables.
Array has two subscripts one denote the row and the other denotes the
column.

Declaration of Array:
The general form of declaration is
data_type variable_name[row_size][column_size];
Example:
int marks[4][3];

The first element can be accessed as marks[0][0], second element as [0]


[1] and so on upto the last element as [3][2].
Two Dimensional Arrays
In memory 12 consecutive storage locations are reserved for the variable marks
as shown below:
marks marks
marks[0] marks[0] marks[0]
90 98 80
[0] [1] [2]

marks[1] marks[1] marks[1]


80 85 77
[0] [1] [2]

marks[2] marks[2] marks[2]


45 65 55
[0] [1] [2]

marks[3] marks[3] marks[3]


76 58 80
[0] [1] [2]

The initialization to the array can be done as below


int marks[4][3] = {90,98,80,80,85,77,45,65,55,76,58,80};
or
int marks[4][3] = {{90,98,80},{80,85,77},{45,65,55},{76,58,80}};
Two Dimensional Arrays
#include<stdio.h> printf(Sum of matrix A and B is\n);
#include<conio.h>
void main()
{ for(i=0;i<r1;i++)
int a[10][10],b[10][10],c[10][10]; for(j=0;j<c1;j++)
int i,j,r1,c1,r2,c2; c[i][j]=a[i][j]+b[i][j];
clrscr(); for(i=0;i<r1;i++)
printf(Enter the order of the matrix {
A:); for(j=0;j<c1;j++)
scanf(%d%d,&r1,&c1);
printf(%d\t,c[i][j]);
printf(Enter the order of the matrix
B:);
printf(\n);
scanf(%d%d,&r2,&c2); }
if(r1!=r2 && c1!=c2) }
printf(Matrix cannot be added \n); }
else
{ Output
printf(Enter the elements of the Enter the order of the matrix A:2 2
matrix A:);
Enter the order of the matrix B:2 2
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
Enter the elements of the matrix
scanf(%d,&a[i][j]); A:1 2 3 4
printf(Enter the elements of the Enter the elements of the matrix
matrix B:) B:1 2 3 4
Character Arrays & Strings
A string is a sequence of characters.
Any sequence or set of characters defined within double
quotation symbols is a constant string.
Declaration of Strings
The general form of declaration of a string variable is
char string_name[size];
The size determines the number of characters in the
string name.
Example
char month[10];
char address[100];
The size of the array should be one byte more than the
actual size of the string since the complier appends a null
character \0 at the end of the string.
Character Arrays & Strings
Initializing Strings:
char month[ ]={j,a,n,u,a,r,y};
This is perfectly valid but C offers a special way to initialize
strings
The above string can be initialized as
char month[ ] = january;
The string stored in the memory as :
j
a
n
u
a
r
y
\0
Character Arrays & Strings
Reading Strings from the terminal:
char address[25];
scanf(%s, address);
The scanf statement has a drawback; it just ignores the input after any blank
space.
If the input is new york only new will be read and since a blank space after new
it will terminate the string.
To overcome this gets(sddress) can be used

Writing Strings to the screen:


char name[25];
scanf(%s, name);
printf(%s, name);
String Handling Functions
Strings Standard Functions: <string.h>

Function Purpose

strlen( ) used to find length of the string

strcpy( ) used to copy one string to another

strcat( ) used to combine two strings

strcmp( ) used to compare characters of two strings

strlwr( ) used to convert strings into lower case

strupr( ) used to convert strings into upper case

strrev( ) used to reverse a string


String Handling Functions
strlen( ) function:
This functions counts and returns the number of characters in a
string.
The length does not include the null character.

The general form is

variable_name = strlen(string);

where variable_name is an integer variable which receives


the length of the string.
Example:

length = strlen(hello world);

the value of the variable length is 11.


String Handling Functions
strcpy( ) function:
In C it is not possible to assign the characters to a string directly
like dept = CSE;.
The strcpy( ) function is used to assign a string as value to
another string.
The general form is
strcpy(string1, string2);
where string1 & string2 are character array.
The content of the string2 is assigned(copied) to string1.
Example2:
Example1: Char dept[5];
char str1[10] = Hello ; strcpy(dept,CSE);
char str2[10] = World!;
strcpy(str1,str2);
In the above statement the content of
str2 is overwritten to the str1.
String Handling Functions
strcat( ) function:
The strcat( ) function joins 2 strings together.
The general form is
strcat(string1, string2);
where string1 & string2 are character array. When the
function is executed the string2 is appended to the string1 and
the string in string2 remains unchanged.
Example:
char str1[ ] = Hello ;
char str2[ ] = World!;
strcat(str1,str2);

The value stored in str1 = Hello World! and str2 = World


remains same.
String Handling Functions
strcmp( ) function:
In C two strings cannot be compared directly like if(str1 ==
str2).
The strcmp( ) function compares two strings and returns a
zero if both the strings are equal or returns a non-zero if both
are not same.
The general form is
strcmp(string1, string2);
where string1 & string2 are character array.
Example:
char str1[10] = Hello ;
char str2[10] = World!;
if(strcmp(str1,str2) == 0)
printf(Both the strings are same);
else
printf(Both the strings are different);
String Handling Functions
strlwr( ) function:
The strlwr( ) function is used to convert all the characters
in a string to lower case.
The general form is

strlwr(string);

where string is a character array.


The content of the string is converted to lowercase.
Example1:

char str1[10] = HELLO;

strlwr(str1);

From the above statement the content of str1 is hello.


String Handling Functions
strupr( ) function:
The strupr( ) function is used to convert all the characters
in a string to upper case.
The general form is

strupr(string);

where string is a character array.


The content of the string is converted to uppercase.
Example1:

char str1[10] = hello;

strupr(str1);

From the above statement the content of str1 is HELLO.


String Handling Functions
strrev( ) function:
The strrev( ) function is used to reverse all the characters in a
string.
The general form is

strrev(string);

where string is a character array.


The content of the string is reversed.

Example1:

char str1[10] = program;

strrev(str1);

From the above statement the content of str1 is margorp.


String Handling Fn. Example
#include < stdio.h > l1=strlen(s1);
#include < string.h > l2=strlen(s2);
void main()
l3=strlen(s3);
{
char s1[20],s2[20],s3[20]; printf(\nS1 = %s\tLength =
int x,l1,l2,l3; %d\n,s1,l1);
printf(Enter the strings:); printf(\nS2 = %s\tLength =
scanf(%s%s,s1,s2); %d\n,s2,l2);
x=strcmp(s1,s2); printf(\nS3 = %s\tLength =
if(x!=0) %d\n,s3,l3);
{ }
printf(\nStrings are not Output
equal\n);
Enter the strings:Hello World
strcat(s1,s2);
} Strings are not equal
else S1 = HelloWorld Length = 10
printf(\nStrings are equal); S2 = World Length = 5
strcpy(s3,s1); S3 = HelloWorld Length = 10
String Handling Fn. Example
#include<stdio.h>
#include<conio.h> else
#include<string.h> printf("\nThe string is a
void main()
palindrome");
{
getch();
char str[15],rev[15];
}
clrscr();
printf("\nEnter a string: ");
Output
scanf("%s",str); Enter a string: HELLO
strcpy(str,rev); The string is not a palindrome
strrev(rev);
if(strcmp(str,rev)) Enter a string: MADAM
printf("\nThe string is not The string is a palindrome
a
strncpy,strncmp,strncat
Strncpy

The general form is


strncpy(string1,string2,n);
Copy string2 to string1 upto specified length n

Example1:
char str1[10] = Hello ;
char str2[10] = Hai;
strncpy(str1,str2,2);
In the above statement the content of first 2 characters of
str2 is overwritten to the str1.
Therefore, str1=Hallo
strncpy,strncmp,strncat
Strncmp

The general form is


strncmp(string1,string2,n);
Compares characters of two strings upto specified
length n

Example1:
char str1[10] = Hello;
char str2[10] = Heal;
strncmp(str1,str2,2);
In the above statement the first 2 characters of str1 and
str2 are compared
strncpy,strncmp,strncat
Strncat

The general form is


strncat(string1,string2,n);
Append source string to destination string upto specified
length n

Example1:
char str1[10] = Hello;
char str2[10] = Heal;
strncat(str1,str2,2);
In the above statement the first 2 characters of str2
appended to str1
Therefore, s1=HelloHe

You might also like