0% found this document useful (0 votes)
9 views

Module 3

This document discusses arrays and strings in C programming. It begins by defining an array as a collection of values of the same type stored in consecutive memory locations. Each value has an index, and arrays can store different data types like integers, floats, characters, and strings. The document then covers declaring and initializing arrays, calculating array element addresses and length, and using one-dimensional arrays. It provides examples and formulas for declaring, initializing, and accessing array elements in C.

Uploaded by

alsonmathias1209
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Module 3

This document discusses arrays and strings in C programming. It begins by defining an array as a collection of values of the same type stored in consecutive memory locations. Each value has an index, and arrays can store different data types like integers, floats, characters, and strings. The document then covers declaring and initializing arrays, calculating array element addresses and length, and using one-dimensional arrays. It provides examples and formulas for declaring, initializing, and accessing array elements in C.

Uploaded by

alsonmathias1209
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

ARRAYS & STRINGS

Module 3

ARRAYS & STRINGS

1 ARRAYS AND STRINGS

1.1 Using an array


1.2 Using arrays with functions
1.3 Multi-Dimensional arrays

1.1 USING AN ARRAY

 An array is a collection of same type of values stored in consecutive memory locations.


 Array can be used to store the values of different data types. Such as int, float, char and
string.
 Each value in an array is referenced by a single name, which is the name of the array and
indicates the position of the value in the array.
 The subscript[ ] is a positive integer number, which is enclosed in pair of square brackets.
 Because of these subscripts, sometimes an array is called as a “subscripted variable”.
 The individual values in an array are called as “elements of the array”.

I) ARRAY DECLARATION :

type name[size];
Where,
 The type can be int, float, double, char or any other valid data type.
 Name to identify the array.
 The number within brackets indicates the size of the array. i.e, the maximum number
of elements that can be stored in the array.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 1


ARRAYS & STRINGS

EXAMPLE:
int marks[5];

 This statement declares a marks variable to be an array containing 5 elements.


 In C, the array index starts from zero.

1st Element 2nd Element 3rd Element 4th Element 5th Element
marks[0] marks[1] marks[2] marks[3] marks[4]

Figure : Memory representation of an array of 5 elements.

 The first element will be stored in marks[0].


 The second element will be stored in marks[1], and so on.
 Therefore, the last element, i.e the 5th element will be stored in marks[4].
 Note that 0,1,2,3 written within square brackets are subscripts.

II) CALCULATING THE ADDRESS OF ARRAY ELEMENTS:

 The address of array elements can simply be calculated by using the base address. The
general formula to calculate the address of array element is

A]K]=BA(A)+W(K-LOWER-BOUND)

Where,
 A-is an array.
 K- index of the element, for which we have to calculate the address.
 BA- base address of the array A.
 W- is the word size of one element in memory(for ex: size of int is 2).
 LOWER-BOUND- is the index of the first element in the array.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 2


ARRAYS & STRINGS

EXAMPLES

1) Given an array int marks[ ]={99,67,78,56,88,90,34,85}. Calculate the address of


marks[4] if base address=1000.

Solution :

99 67 78 56 88 90 34 85
marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]

Storing an integer requires 2 bytes, therefore the word size is 2 bytes.

A]K]=BA(A)+W(K-LOWER BOUND)
marks[4]=1000+2(4-0)
=1000+8
=1008

2) Given an array float avg[ ]={99.0,67.0,78.0,56.0,88.0,90.0,34.0,85.0}. Calculate the


address of avg[4] if base address=1000.

Solution :

99.0 67.0 78.0 56.0 88.0 90.0 34.0 85.0


marks[0] marks[1] marks[2] marks[3] marks[4] marks[5] marks[6] marks[7]

Storing an float requires 4 bytes, therefore the word size is 4 bytes.

A]K]=BA(A)+W(K-LOWER BOUND)
marks[4]=1000+4(4-0)
=1000+16
=1016

Note: Storing an char requires 1 byte, therefore the word size is 1 byte.
Storing an double requires 8 bytes, therefore the word size is 8 bytes.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 3


ARRAYS & STRINGS

III) INITIALIZATION OF AN ARRAY:

 An array must be both, declared and defined before it can be used. Array declaration only
reserves memory for the elements in the array. No values will be stored.
 The values of array elements are stored when an array is defined is called as “array
initialization”.
 The general form of array initialization is :

type array_name[size]={value 0, value 1….value n-1};

Where,
 The type can be int, float, double,char or any other valid data type.
 Name to identify the array.
 The number within brackets indicates the size of the array. i.e, the maximum number
of elements that can be stored in the array.
 The values are specified in the curly braces and they are separated by commas.

Ex: 1) int marks[5]={90,82,78,95,88};

90 82 78 95 88
marks[0] marks[1] marks[2] marks[3] marks[4]

An array with name marks is declared, that has enough space to store 5 elements.
The first element, i.e marks[0] is assigned with value 90. The second element, i.e
,marks[1] is assigned with value 82 and so on.

Ex: 2) int marks[5]={90,82};

90 82 0 0 0
marks[0] marks[1] marks[2] marks[3] marks[4]

If the number of values provided is less than the number of element in the array,
then the un-assigned elements are filled with zeros.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 4


ARRAYS & STRINGS

IV) CALCULATING THE LENGTH OF THE ARRAY:

 The length of array is given by the number of elements stored in it. The general formula
to calculate the length of the array is

LENGTH=UPPER BOUND - LOWER BOUND+1

Where,
 UPPER BOUND- is the index of the last element.
 LOWER BOUND- is the index of the first element in array.

EXAMPLE:

1) Let age be an array of integers such that age[0]=2, age[1]=5, age[2]=3, age[3]=1
age[4]=7. Show the memory representation of the array and calculate its length.

Solution : Memory Representation

2 5 3 1 7
age[0] age[1] age[2] age[3] age[4]

LENGTH=UPPER BOUND - LOWER BOUND+1


=4-0+1
=5

V) ONE-DIMENSIONAL ARRAY :

 A one dimensional array or single dimensional array is used to store a linear list of values
of the same type.
 The elements in a one dimensional array are stored with the index values starting from
zero to one less than size of the array.
 Each element in a one dimensional array is accessed using the same name and a single
subscript[ ].
 A one dimensional array is similar to a row or column matrix.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 5


ARRAYS & STRINGS

 DECLARATION:
type name[size];

Where,
 The type can be int, float, double,char or any other valid data type.
 Name to identify the array.
 The number within brackets indicates the size of the array. i.e, the maximum number
of elements that can be stored in the array.

Examples:
1) int list[10] – declaration creates an array named list of 10 integer constants.
2) char name[20] – declaration creates name as an array of 20 characters.
3) float xyz[5] – declaration creates xyz as an array of 5 floating point numbers.
4) double p[100] – declaration creates an array of 100 double precision numbers.

 TOTAL SIZE
The total amount of memory that can be allocated to a one dimensional array is
computed as
total size = size * [size of(data_type)];

Where,
 size- is a number of elements of a one dimensional array.
 size of( ) – is an unary operator to find the size in bytes.
 data_type – basic data or user defined data type.

 INITIALIZATION OF ONE-DIMENSIONAL ARRAY

type array_name[size]={value 0, value 1….value n-1};

Where,
 The type can be int, float, double,char or any other valid data type.
 Name to identify the array.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 6


ARRAYS & STRINGS

 The number within brackets indicates the size of the array. i.e, the maximum number
of elements that can be stored in the array.
 The values are specified in the curly braces and they are separated by commas.

THE VARIOUS WAYS OF INITIALIZING ONE DIMENSIONAL-ARRAYS ARE AS


FOLLOWS :
a) Initializing all specified memory locations.
b) Partial array initialization.
c) Initialization without size.
d) Array initialization with a string.

a) Initializing all specified memory locations:


Array elements can be initialized with data items of type int, char, float etc. The values
are copied into array in the order specified in the declaration.

Ex: 1) int marks[5] = {10,15,20,25,30}

10 15 20 25 30
m[0] m[1] m[2] m[3] m[4]
Figure : Memory representation of array
An array with name marks is declared, that has enough space to store 5 elements. The
first element, i.e marks[0] is assigned with value 10. The second element, i.e ,marks[1] is
assigned with value 15 and so on.

2) int a[5] = {10,12,15,16,17,50,30,80}


It is a compiler error to specify more number of elements in the array. Number of
initial values are more than the size of array.

b) Partial array initialization:

If the number of values to be initialized is less than the size of the array, then the
elements are initialized in the order from 0th location. The remaining locations will be initialized
to zero automatically.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 7


ARRAYS & STRINGS

Ex: int marks[5]={90,82};

90 82 0 0 0
marks[0] marks[1] marks[2] marks[3] marks[4]

c) Initialization without size:

int marks[ ]={98,97,90};

98 97 90
m[0] m[1] m[2]
 Here, the compiler will calculate the size of the array based on the number of initial
values.
 Since number of elements is 3, totally 3*2=6 bytes are reserved. Where size of integer is
2 bytes.

d)Array initialization with a string:


 A string is a sequence of characters, which is enclosed within a double quotes.
 A string is always terminated by NULL character, which is denoted by „\0‟.
 Consider declaration with string initialization:

char b[ ]=”COMPUTER”;
The array b is initialized as

C O M P U T E R \0
b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7] b[8]

Ex: 1) char b[9]=”COMPUTER”;


Array size is greater than or equal to string length+1(size=string length=9). So this
declaration is correct.
2) char b[8]=”COMPUTER”;
Array size is not greater than or equal to string length+1(size=8, string length=9). So
this declaration is wrong.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 8


ARRAYS & STRINGS

This program illustrates Reading and Writing of One Dimensional Array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5], i, n;
printf(“enter the size of the array\n”);
scanf(“%d”,&n);
printf(“enter the elements of the array\n”);
for(i=0;i<n;i++)
scanf(“%d”, &a[i]);
printf(“elements of the array are\n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
}

OUTPUT: Enter the size of the array: 3


Enter the elements of the array : 10 20 30
Elements of the array are:
10
20
30

1.2 TWO DIMENSIONAL ARRAYS

 The arrays with more than one dimension are called “Multi-Dimensional Arrays”.
 Multi-dimensional arrays are defined in much the same manner as one-dimensional
arrays, except that a separate pair of square brackets is required for each subscript.
 A multi-dimensional array is used, when data items are arranged in row-wise and
column-wise in a tabular fashion.
 In multi-dimensional array, to identify a particular item it is necessary to specify two
subscripts. The first subscripts identifies the row number and second subscripts identifies
the column number.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 9


ARRAYS & STRINGS

 DECLARATION :

data_type array_name[row size] [column size];

Where,
 The type can be int, float, double,char or any other valid data type.
 Name to identify the array.
 Row size- number of elements to be processed under subscript 1.
 Column size- number of elements to be processed under subscript 2.

Example: int a[3][4];


 The array a has two sets of square brackets[ ][ ] and hence it is a multi-dimensional array
with 3 rows and 4 columns.
 This declaration informs the compiler to reserve 12 locations(3*4=12).

a[0][0] a[0][1] a[0][2] a[0][3]


a[1][0] a[1][1] a[1][2] a[1][3]
a[2][0] a[2][1] a[2][2] a[2][3]

 INITIALIZATION:

 The values of array elements are stored when an array is defined is called as “array
initialization”.
 The initialization can be done at the time of declaration using the following syntax.

data_type array_name[size 1][size 2]={{a1,a2,………an},


{b1,b2,………bn},
…………………
{z1,z2,………zn}};

Where,
 size1 and size2 can be integer constants or integer expressions.
 a1 to an are the values assigned to 1st row.
 b1 to bn are the values assigned ti 2nd row and so on.
 a,b & z – initial values to be assigned to n elements of array.

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 10


ARRAYS & STRINGS

THE VARIOUS WAYS OF INITIALIZING MULTI-DIMENSIONAL ARRAYS ARE AS


FOLLOWS :
a) Initializing all specified memory locations.
b) Partial array initialization.

a) Initializing all specified memory locations:

int a[4][3]={{11,22,33},
{44,55,66},
{77,88,99},
{10,11,12}};

The declaration indicates that array a has 4 rows and 3 columns. The memory
representation of this array is shown below.

11 22 33
44 55 66
77 88 99
10 11 12

A[0][0]=11 a[1][0]=44 a[2][0]=77 a[3][0]=10


A[0][1]=22 a[1][1]=55 a[2][1]=88 a[3][1]=11
A[0][2]=33 a[1][2]=66 a[2][2]=99 a[3][2]=12

b) Partial array initialization:


If the number of values to be initialized are less than the size of the array, then the
elements are initialized from left to right one after the other. The remaining locations will be
initialized to zero.
Example: int a[4][3]={{11,22},
{44,55},
{77,88},
{10,11}};

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 11


ARRAYS & STRINGS

The declaration indicates that array a has 4 rows and 3 columns in which only first two
columns of each row are initialized. The 3rd column of each row will be initialized with zeros
automatically. The memory representation of this array is shown below.

11 22 0
44 55 0
77 88 0
10 11 0

a[0][2], a[1][2], a[2][2], a[3][2] are initialized with zero‟s.


This program illustrates Reading and Writing of Two Dimensional Array
#include<stdio.h>
#include<conio.h>
void main( )
{
int a[10][10],i,j,m,n;
printf(“enter the number of rows in the array\n”);
scanf(“%d”,&m);
printf(“enter the number of columns in the array\n”);
scanf(“%d”,&n);
printf(“enter the elements of the array\n”);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf(“%d”,&a[i][j]);
printf(“ elements of the array are\n”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(“%d”, a[i][j]);

printf(“\n”);
}
}

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 12


ARRAYS & STRINGS

OUTPUT: enter the number of rows-3


enter the number of columns-3
enter the elements of the array- 5 6 8 2 4 3 1 9 7
elements of the array are- 5 6 8
243
197
2 STRINGS

 A string is one-dimensional character array, where each element is either a character


constant or a string constant.

Example: Create an array of characters that stores the vowels of English language.
Then, the vowels characters(a,e,i,o,& u) are either character constants or string constants.

 String variable is often called as “character array”. A technique of storing more than one
character in a single variable with similar type data refers to “character array”.
 Memory to character or string array gets allocated from 0th position.
 When string variable is declared, size should be specified with minimum of 1 location
and maximum of 65,535 location.
 By default every string is terminated by null character.

Example: “COMPUTER”

C O M P U T E R \0
Ch[0] ch[1] ch[2] ch[3] ch[4] ch[5] ch[6] ch[7] ch[8]

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 13


ARRAYS & STRINGS

2.1 DECLARING AND INITIALIZING STRING VARIABLES

I) DECLARATION:

Strings can be represented as character arrays.

char string_name[size];

Where,

 String_name- user defined string name.


 Size – determines the number of characters to be stored inside the string name.

Example:
char name[30];
In above example, it is possible to store size n-1 characters in the array because the last
character would be the null character. So char name[30] can store a maximum of 29 usable
characters.

II) INITIALIZATION:

The general form of initializing an string variable is

char string_name[size]={„H‟,‟E‟,‟L‟,‟L‟,‟O‟,‟\0‟};

Where,
 String_name- user defined string name.
 Size – determines the number of characters to be stored inside the string name.
 Size of the string is not mentioned in above example. Here, the compiler will
automatically calculate the size based on the number of elements initialized.

char string_name[10]=“HELLO”;
 In such cases, the compiler creates a character array of size 10; stores the value
“HELLO” in it, and finally terminates the value with a null character.
 Rest of the elements in the array are automatically initialized to NULL.

H E L L O \0 \0 \0 \0 \0
Ch[0] ch[9]

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 14


ARRAYS & STRINGS

2.3 READING and WRITING STRINGS

I) READING STRINGS:

A string data can be read by using three ways:

a) scanf(function).
b) gets( ) function.
c) getchar( ) or getch( ) function.

a) scanf( ) function:
 The scanf( ) function reads characters from the keyboard and stores them in variables
according to the specified formats, which are known as format specifiers(%s).
 The string can be read using scanf( ) as follows

scanf(“%s”, str);

 The main pitfall with this function is that, it terminates as soon as it finds a blank space.
Example: If the user enters WELCOME TO DSATM, then str will contain only
WELCOME. This is because the moment a blank space is encountered, the string is
terminated by the scanf( ) function.

b) gets( ) function:
 The string can be read using gets( ) as follows

gets( str);

 gets() is a simple function that overcomes the drawbacks of scanf( ).


 the gets( ) function takes the starting address of the string, which will hold the input.
 the string inputted using gets( ) is automatically terminated with a null character.
 gets( ) is used to read a sequence of characters from the keyboard with spaces in between
and store them in memory location.
Example : char str[10];
gets(str);
Where,
str- string name.
Ex: ABC

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 15


ARRAYS & STRINGS

c) getchar or getch( ) function:


 To read single character from the terminal, getch( ) function is used.
 getch() is used to repeateadly to read a sequence of single characters and simultaneously
stores it in a character array.
 The string can be read using getch( ) as follows:

char ch;
ch=getchar( );
Ex: „A‟

II) WRITING STRINGS:

The following output functions can be used to write or print a string:

a) printf(function).
b) puts( ) function.
c) putchar( ) function.

a) printf( ) function:
 The string can be displayed using printf( ) as follows

printf(“%s”, str);

 The above syntax displays the entire contents of the arrays. The format specifier %s can
be used to display an array of characters that is terminated by the null character.

b) puts( ) function:
 The string can be displayed using puts( ) as follows

puts( str);

 puts( ) is a simple function that overcomes the drawbacks of printf( ).


 the puts( ) function terminates the line with a newline character(„\n‟).
Example : ABC, A BC

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 16


ARRAYS & STRINGS

c) putchar( ) function:
 The string can be displayed using putchar( ) repeatedly to print a sequence of single
characters.

putchar( ch);
Ex: char ch=‟A‟;

putchar(name);
Ex: char name={„R‟,‟A‟,‟M‟,‟A‟};

 Whenever putchar( ) is executed, the data stored in a memory location identified by a


variable “ch”.
 Name is read from the memory and will be displayed on the screen.

C program using the scanf( ) and printf( ) functions

#include<stdio.h>
#include<conio.h>
void main()
{
char name[20];
printf(“what is your name ?”);
scanf(“%s”,&name);
printf(“welcome, %s\n”, name);
}
OUTPUT: What is your name ? Charu
Welcome, charu

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 17


ARRAYS & STRINGS

C program using the gets( ) and puts( ) functions

#include<stdio.h>
#include<conio.h>
#define length 100
void main()
{
char student_name1[length]=”john”;
char student_name2[length]=”regi”;
puts(“name of 1st student”);
puts(student_name1);
puts(“name of 2nd student”);
puts(student_name2);
puts(“enter a new name for the second student”);
gets(student_name2);
puts(student_name1);
puts(student_name2);
}

OUTPUT: Name of 1st student


John
Name of 2nd student
Regina
Enter a new name for second student
Keerthana
John
Keerthana

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 18


ARRAYS & STRINGS

C program using the getchar( ) and putchar( ) functions

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf(“enter a word\n”);
ch=getchar( );
putchar(ch);

}
OUTPUT: enter a word
DSATM
D

2.4 STRING HANDLING FUNCTIONS


a) strcpy(dest,src)
b) strncpy(dest,src,n)
c) strcat(str1,str2)
d) strncat(str1,str2,n)
e) strcmp(str1,str2)
f) strncmp(str1,str2,n)
g) strlen( )
h) strrev( )
i) strlwr( )
j) strupr( )
k) strchr( )

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 19


ARRAYS & STRINGS

a) strcpy(dest,src)

 The strcpy( ) function is used to copy the content of one string to another string.
 The string to be copied can be a string which is already stored in an array of character.
 The strcpy function will copy the entire string including the terminology null character, to
the new location.

 SYNTAX :
strcpy(dest, source);

Where,
 strcpy function has two parameters.
 Destination – a string vaiable, whose value is going to be changed.
 Source – the string variable, which is going to be copied to the destination.

C program which illustrates strcpy( ) function

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[10], str2[10]=”hello”;
strcpy(str1,str2);
printf(“\n str1:%s”,str1);
}
OUTPUT : hello

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 20


ARRAYS & STRINGS

b) strncpy(dest,src,n)

 The strncpy( ) function is used to copy a specified number of characters from one string
to another string.

 SYNTAX :

strncpy(dest,source,n);

Where,
 strncpy( ) function has three parameters, where first two parameters are strings, which
have to be copied and the third parameter is number of characters, up to which the
second string is copied to the first string.

C program which illustrates strncpy( )function.

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[100], str2[100]=”hello”;
strncpy(str1,str2,2);
printf(“\n str1:%s”,str1);
}
OUTPUT : he

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 21


ARRAYS & STRINGS

c) strcat(str1,str2)

 The strcat( ) function is used to concatenate, or join together, two strings.

 SYNTAX :

strcat(string_data1, string_data2);

Where,
 strcat( ) function has two arguments to add second string to first string. This means
that second string is appended to first string and the concatenated string is stored in
the first string.
C program which illustrates strcat( ) function.

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[50]=”programming”;
char str2[ ]=”in c”;
strcat(str1,str2);
printf(“\n str1:%s”,str1);
}
OUTPUT : programming in c

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 22


ARRAYS & STRINGS

d) strncat(str1,str2,n)

 The strncat( ) function is used to add two strings, where a specified number of
characters of one string are appended at the end of another string.

 SYNTAX :

strncat(string_data1, string_data2, n);

Where,
 strncat( ) function has three parameters, where 2 parameters are the strings, which
have to be concatenated. The third parameter is number of characters upto which the
two strings are combined.

C program which illustrates strncat( ) function.

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[50]=”programming”;
char str2[ ]=”in c”;
strncat(str1,str2,2);
printf(“\n str1:%s”,str1);
}
OUTPUT : programming

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 23


ARRAYS & STRINGS

e) strcmp(str1,str2)

 The strcmp( ) function is used to compare two strings.


 This function performs a case sensitive comparisons between strings s1 and s2 and
returns an integer value as follows.
 A zero value indicates that the both strings are equal.
 A value, which is greater than zero, indicates that the first character that does not
match has a greater value in s1 than in s2.
 A value, which is less than zero, indicates that the first character that does not match
has a lesser value in s1 than in s2.

 SYNTAX :
strcmp(string_data1, string_data2);
Where,
 strcmp( ) function takes two parameters, where 2 parameters are the strings, which
has to be compared.

C program which illustrates strcmp( ) function.


#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[10]=”HELLO”;
char str2[10]=”HEY”;
if(strcmp(str1,str2)==0)
printf(“strings are identical\n”);
else
printf(“strings are not identical\n”);
}
OUTPUT : strings are not identical

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 24


ARRAYS & STRINGS

f) strncmp(str1,str2,n)

 The strncmp( ) function is used to compare two strings up to a specified number of


characters.

 SYNTAX :

strncmp(string_data1, string_data2,n);
Where,
 strncmp( ) function takes three parameters, where first two parameters are the strings,
which have to be compared, and the third parameter is the number of characters up to
which the two strings are compared.

C program which illustrates strncmp( ) function.

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[10]=”HELLO”; char
str2[10]=”HEY”;
if(strncmp(str1,str2,2)==0)
printf(“strings are identical\n”);
else
printf(“strings are not identical\n”);
}
OUTPUT : strings are identical

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 25


ARRAYS & STRINGS

g) strlen(str)

 The strlen( ) function is used to find the length of the string in bytes.
 The size of the operator can be used to determine the declared size of a string.
 It accepts one variable or operand and returns the number of bytes allocated to it.

 SYNTAX :
strlen(string_data);

Where,
 strlen( ) function takes a string parameter to count the number of characters in the
string.

C program which illustrates strlen( ) function

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str[ ]=”hello”;
strlen(str);
printf(“\n length of string is %d”);
}
OUTPUT : string length=5

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 26


ARRAYS & STRINGS

h) strrev( )

 The strrev( ) function is used to reverse a string, which takes one string argument.

 SYNTAX :
strrev(string_data);

Where,
 strrev( ) function takes a parameter of string data, and returns the resultant string by
reversing the order of all the characters.

C program which illustrates strrev( ) function

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str1[100 ];
printf(“enter string”);
gets(string1);
str1=strrev(str1);
printf(“the reverse string is=%s”,string1);
}
OUTPUT : enter string SSIT
reverse string TISS

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 27


ARRAYS & STRINGS

i) strlwr( )

 The strlwr( ) function is used to convert the characters stored in a string into the lower
case.

 SYNTAX :
strlwr(string_data);

Where,
 strlwr( ) function takes a parameter of string data, and returns the resultant string by
converting all the characters into lower case.

C program which illustrates strlwr( ) function

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str[ ]={“WELCOME TO SSIT”};
str1=strlwr(str);
printf(“string in lowercase:%s”);
}
OUTPUT : welcome to ssit

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 28


ARRAYS & STRINGS

j) strupr( )

 The strupr( ) function is used to convert the characters stored in a string into the upper
case.

 SYNTAX :
strupr(string_data);

Where,
 strupr( ) function takes a parameter of string data, and returns the resultant string by
converting all the characters into upper case.

C program which illustrates strupr( ) function

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char str[ ]={“welcome to ssit”};
str1=strupr(str);
printf(“string in uppercase:%s”);
}
OUTPUT: WELCOME TO SSIT

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 29


ARRAYS & STRINGS

k) strchr( )

 The strchr( ) function is used to search a specific character in a string.

 SYNTAX :
strchr(string_data, searching_character);

Where,
 strchr( ) function takes two parameters, one is string data and another is the character
that has to be searched in the string data.

C program which illustrates strchr( ) function

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main( )
{
char *p;
p=strchr(“WELCOME TO CSE BRANCH”,‟C‟);
printf(“p”);
}
OUTPUT: C BRANCH

Dr. Dinesha L , Associate Professor, Dept. of CSE, MITE Page 30

You might also like