0% found this document useful (0 votes)
10 views28 pages

UNIT 3 Arrays Strings

This document provides an introduction to arrays and strings in programming, focusing on the definition, properties, and usage of one-dimensional and two-dimensional arrays. It includes details on array declaration, initialization, accessing elements, and examples of programs for sorting and performing operations like addition, subtraction, and multiplication on arrays. The document is authored by Dr. Ratna Raju Mukiri from the Department of CSE, SACET.

Uploaded by

pavancharan576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views28 pages

UNIT 3 Arrays Strings

This document provides an introduction to arrays and strings in programming, focusing on the definition, properties, and usage of one-dimensional and two-dimensional arrays. It includes details on array declaration, initialization, accessing elements, and examples of programs for sorting and performing operations like addition, subtraction, and multiplication on arrays. The document is authored by Dr. Ratna Raju Mukiri from the Department of CSE, SACET.

Uploaded by

pavancharan576
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

INTRODUCTION TO PROGRAMMING

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.

ONE - DIMENSIONAL ARRAY


 A list of items specified under one variable name using only one
subscript is called a single-subscripted variable
 It is known as one-dimensional array or 1D array
 It is also called as Single dimensional array.
 It is simply called as array.
 The subscript begins with the number 0.
 For Example, if we want to represent a set of five numbers, say (30,
45, 55, 10, 73) as an array variable marks.
 We may declare the variable marks as follows:
int marks[5];
 Now the computer stores the values of the array marks after reserving
five storage locations and assigning the values to array elements.

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
1
INTRODUCTION TO PROGRAMMING

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

Declaration of one dimensional Array


 An array variable is declared by specifying first the base type of the
array, then the name of the array variable, and then the number of
elements of the array that is specified between a pair square brackets
([]).
 The general format of array declaration is
type array_name[size];
 Ex:
int marks [100];
 Here int specifies the data type and marks specifies the name of the
array and size specifies the maximum size of the array.
 From the above example the maximum size of the array is 100.

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
2
INTRODUCTION TO PROGRAMMING

Array Indexing / Accessing elements of the Array


 Once the array is declared it is possible to refer the individual
elements of the array.
 This is done with the subscript which contains a number in the
brackets following the array name. The number specifies the element
position in the array.
 All the array elements are numbered starting from 0.
 Thus marks[2] specifies the third element of the array.

INPUT AND OUTPUT OF ARRAY VALUES


Entering data into an array/ Reading an array
for(i=0;i<10;i++)
{
printf(“ enter the marks”);
scanf(“%d”,&marks[i]);
}

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.

Printing data into an array


for(i=0;i<10;i++)
{
printf(“%d”, marks[i]);
}
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 printing the students marks. For the first time the value of “i” is 0
printed through printf() statement and print the value of marks[0] which is

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
3
INTRODUCTION TO PROGRAMMING

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

Compile time initialization


 Elements of an array can be assigned initial values at the time of
compilation known as compile time initialization.
 The general format of initialization is
type array_name[size]={list of values};

 Ex: int marks [5] = {65, 98, 62, 48, 57};


 Defines the array marks to contain five integer elements and initializes
marks [0] to 65, marks [1] to 98, marks [2] to 62, marks [3] to 48 and
marks [4] to 57.
 If the number of initializes is less than the number of element in the
array, the remaining elements are set zero.
 Ex:
int m [5] = {3, 4, 8}; is equivalent to
int m [5]= {3, 4, 8, 0, 0};
 If initializes have been provided for an array, it is not necessary to
explicitly specify the array length, in which case the length is derived
from the initializers.
 A character array may be initialized by a string constant
 We can also initialize with single character constants such that the
last character should be a terminating ‘\0’.
 Ex:
char name [10] =”COMPUTERS”;

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
4
INTRODUCTION TO PROGRAMMING

char name[10] = {‘c’, ’o’, ’m’, ’p’, ’t’, ’e’, ’r’, ’s’, ‘\0’};

Run time initialization


 Arrays are initialized at run time also.
 This approach is used when large arrays are considered.
 Ex:
for(i=0;i<100;i++)
{
if(i<50)
sum[i]=0;
else
sum[i]=1;
}

 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]);

Memory Model / Storing an Array


Let us consider the array declaration
int a[10];
 Now 40 bytes get immediately reserved in memory because integer
occupies 4 bytes of memory.
 Array elements occupy contiguous memory locations.
 The diagrammatic representation of how the array elements are stored
in the array after initialization is shown below.
int a[10]={1,2,3,4,5,6,7,8,9,10};
a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
1 2 3 4 5 6 7 8 9 10
2000 2004 2008 2012 2016 2020 2024 2028 2032 2036
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
5
INTRODUCTION TO PROGRAMMING

PROGRAMS WITH ARRAY OF INTEGERS


Program to sorting arrays elements
#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[10], n, i, j, t;
clrscr();
printf("enter the array size");
scanf("%d", &n);
printf("enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
printf(" the elements after sorting are");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
6
INTRODUCTION TO PROGRAMMING

return 0;
}

Program to find Max and Min element in the array


#include<stdio.h>
#include<conio.h>
int main(void)
{
int n, a[10], i, large, small;
clrscr();
printf("enter the array size");
scanf("%d", &n);
printf(" enter the array elements");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
large=small=a[0];
for(i=0;i<n;i++)
{
if(a[i]>large)
large=a[i];
else
if(a[i]<small)
small=a[i];
}
printf("%d\t%d", large, small);
return 0;
}

TWO - DIMENSIONAL ARRAYS


 If an array contains two dimensions then it is called two dimensional
arrays.
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
7
INTRODUCTION TO PROGRAMMING

 This two dimensional array is also called as matrix.


 It is called as array of arrays.
 Since it contains two subscripts it is also known as double
subscripted variable.
 It is used to represent table of values containing row values and
column values.

Two dimensional Array Declaration


 The two-dimensional array can be declared by specifying first the base
type of the array, then the name of the array variable, and then the
number of rows and column elements the array should be specified
between a pair square brackets ([] []).
 The general syntax is
type array_name[row size][column size];
 Ex:
int student[4][3];
 Here int specifies the data type and student specifies the name of the
array and size specifies the row size and column size of the array.
 From the above example there are 4 rows and 3 columns for the array
student.

Two dimensional Array initialization


 Elements of an array can be assigned initial values by following the
array definition with a list of initializes enclosed in braces and
separated by comma.
 Ex:
int marks [2] [3] = {
{65, 98, 62},
{48, 57, 40}
};
 Defines the array marks to contain six integer elements and initializes
marks [0] [3] to 62, marks [1] [1] to 48, marks [1] [2] to 57 and marks
[1] [3] to 40.
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
8
INTRODUCTION TO PROGRAMMING

INPUT AND OUTPUT OF 2D - ARRAY VALUES


Entering data into 2D array/ Reading 2D arrays
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
scanf(“%d”,&marks[i][j]);
}
Printing data into an array
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
printf(“%d”, marks[i][j]);
}

Memory Model / Storing two dimensional Array


Consider the following example to explain about storing two dimensional
arrays.

Program to perform matrix addition


#include<stdio.h>
#include<conio.h>
int main(void)
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
9
INTRODUCTION TO PROGRAMMING

{
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;
}

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
11
INTRODUCTION TO PROGRAMMING

Program to perform matrix multiplication


#include<stdio.h>
#include<conio.h>
int main(void)
{
int a[2][2],b[2][2],c[2][2],n,m,i,j,k;
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 multiplication is ");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
12
INTRODUCTION TO PROGRAMMING

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;
}

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
14
INTRODUCTION TO PROGRAMMING

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

Fixed Length Strings


 In fixed length string format, the first decision is size of the variable.
 If it is too small, we cannot store all data. If it is too big, we waste
memory.
 The problem is how to tell the data from nondata. The solution is to
add nondata at the end of the data.

Variable Length Strings


 A variable length strings can expand or contract to contain the data.
 To store a person name consisting of five characters provide five
character space as well a person name consisting of 20 character
provide 20 character space to contain it.
 There are two techniques for variable length strings
 Length controlled strings
 Delimited strings

Length Controlled Strings


 It has a count that specifies the number of characters in the string.
 This count is used by the string function to determine the length of
the string.

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
15
INTRODUCTION TO PROGRAMMING

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:

 We need to observe the difference about storing a character and a


string in memory
 The character requires one memory location and string requires two
memory locations since one memory location for data and one for null.

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
16
INTRODUCTION TO PROGRAMMING

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

Declaration of strings / character array declaration


 A string is declared as array of characters hence the general form of
string declaration is as follows:
type string_name[size];
in the above syntax the size specifies the number of characters in the
string_name. For example
char city[10];
 We can declare the string as a pointer as follows:
char *ptr;
here the ptr is a pointer variable and the memory is allocated
dynamically for it only not for the string.
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
17
INTRODUCTION TO PROGRAMMING

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’};

When we specify the list of character array elements we need to


supply the null character to indicate the end of the strings. The C language
also permits to initialize the strings without specifying the size. It is shown
as follows:
char month[ ]= “January”;
We can have a string literal assigned to a character pointer as shown
below:
char *pStr = “Good Day”;

STRING INPUT AND OUTPUT FUNCTIONS


We have two set of functions to read and write string
1. formatted input / output functions (scanf / fscanf & printf / fprintf)
2. string input // output functions (gets / fgets & puts / fputs)

String Input / Output Functions


C has two sets of string functions to read and write.
1. Line to string
2. String to line

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
18
INTRODUCTION TO PROGRAMMING

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.

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
19
INTRODUCTION TO PROGRAMMING

STRING MANIPULATION FUNCTIONS


The following are the string handling functions. They are used to carry
out operations on string known as string manipulations.
 String concatenation – strcat() – used to concatenate two strings
 String comparison – strcmp() – used to compare two strings
 String copy – strcpy() – used to copy one string over another string
 String length – strlen() – used to find the length of the string

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

Program to illustrate about string concatenation using string library


function
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
Dr. Ratna Raju Mukiri Ph.D.,
Dept. of CSE, SACET.
20
INTRODUCTION TO PROGRAMMING

char str1[10], str2[10];


clrscr();
printf(“Enter the string”);
gets(str1);
printf(“Enter the string”);
gets(str2);
strcat(str1,str2);
puts(str1);
puts(str2);
retun 0;
}

Program to illustrate about string concatenation without using string


library function
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main(void)
{
char s1[50], s2[50], i, j;
clrscr();
printf(“Enter the string”);
gets(s1);
printf(“Enter the string”);
gets(s2);
for(i=0; s1[i]!='\0'; ++i) /* i contains length of string s1. */
for(j=0; s2[j]!='\0'; ++j, ++i)
s1[i]=s2[j];
s1[i]='\0';
printf("After concatenation: %s",s1);
return 0;
}

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
21
INTRODUCTION TO PROGRAMMING

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 you consider the third statement such as strcmp(“rom”,”ram”).


The second characters in both the strings are different then we have to
consider their ASCII values to get the result. The result may be zero, positive
or negative.

Program to illustrate about string comparison using string library


function
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
int x;
char str1[10], str2[10];
clrscr();
printf(“Enter the string”);
gets(str1);
printf(“Enter the string”);
gets(str2);
x=strcmp(str1,str2);

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
22
INTRODUCTION TO PROGRAMMING

if(x==0)
printf(“the strings are equal”);
else
printf(“the string are not equal”);
return 0;
}

Program to illustrate about string comparison without using string


library function
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main(void)
{
char string1[5],string2[5];
int i, temp = 0;
clrscr();
printf(“Enter the string”);
gets(string1);
printf(“Enter the string”);
gets(string2);
for(i=0; string1[i]!='\0'; i++)
{
if(string1[i] == string2[i])
temp = 1;
else
temp = 0;
}
if(temp == 1)
printf("Both strings are same.");
else
printf("Both string not same.");
return 0;

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
23
INTRODUCTION TO PROGRAMMING

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”);

will assign the string “delhi” to the string variable city.

Program to illustrate about string copy using string library function


#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
char str1[10], str2[10];
clrscr();
printf(“Enter the string”);
gets(str1);
printf(“Enter the string”);
gets(str2);
strcpy(str1,str2);
puts(str1);
puts(str2);
return 0;
}

Program to illustrate about string copy without using string library


function
#include<stdio.h>

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
24
INTRODUCTION TO PROGRAMMING

#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”);

Program to illustrate about string length using string length function


#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
int x;

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
25
INTRODUCTION TO PROGRAMMING

char str1[10];
clrscr();
printf(“Enter the string”);
gets(str1);
x=strlen(str1);
printf(“ the length of the string is %d”,x);
return 0;
}

Program to illustrate about string length without using string length


function.
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main(void)
{
int i;
char str1[100];
clrscr();
printf(“Enter the string”);
gets(str1);
i=1;
while(i !=’\0’)
{
i++;
}
printf(“ the length of the string is %d”,i);
return 0;
}

strrev() function
The strrev() function is used to find the reverse of the given string. It
takes the following form:

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
26
INTRODUCTION TO PROGRAMMING

strrev(string);

Program to illustrate about string reverse using string library function


#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
int main(void)
{
char str1[10];
clrscr();
printf(“Enter the string”);
gets(str1);
strrev(str1);
puts(str1);
return 0;
}

Program to illustrate about string reverse without using string library


function.
#include<stdio.h>
#include<string.h>
#include<conio.h>
int main(void)
{
int i, x;
char str1[100];
clrscr();
printf(“Enter the string”);
gets(str1);
for(i=0;str1[i] !=’\0’;i++);
x=i-1;
for(i=x;i>=0;i--)

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
27
INTRODUCTION TO PROGRAMMING

printf(“%c”,str1[i]);
return 0;
}

Dr. Ratna Raju Mukiri Ph.D.,


Dept. of CSE, SACET.
28

You might also like