0% found this document useful (0 votes)
5 views90 pages

Arrays in C

Uploaded by

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

Arrays in C

Uploaded by

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

Arrays in C:

Data types

Prepared by Mrs G.S.Patil


Examples of variable declaration

Prepared by Mrs G.S.Patil


Consider the following issue:

• "We have a list of 1000 students


marks of an integer type. If using
the basic data type (int), we will
declare something like the
following…"

• int studMark0, studMark1,


studMark2, ..., studMark999;

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. Program to Display Five intergers number using Array datatypes.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter Array Elements: ");
for (i=0;i<5;i++)
{
scanf("%d ",&a[i]);
}
printf("\n Array Elements: ");
for (i=0;i<5;i++)
{
printf("%d ",a[i]);
}
getch();
}
 Can you imagine how long we have to write the
declaration part by using normal variable
declaration?
– int main(void)
– {
• int studMark1, studMark2, studMark3, studMark4, …, …,
studMark998, stuMark999, studMark1000;
• …
• …
• getch();
– }

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Some examples:
• List of employees in an organization.
• List of products and cost sold by the store.
• Test scores of class of students.
• List of customers and their telephone
numbers .
• And so on...........

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


ARRAYS:
• An array is a collection of data that holds fixed
number of values of same type.
• It is a Derived data type.
• It is a simple and fast way of storing multiple values
under a single name.
• An array in C is a fixed-size collection of similar data
items stored in continuous memory locations.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


ARRAYS:
• The size and type of arrays cannot be changed after its
declaration. .
• An array in C is a fixed-size collection of similar data
items stored in continuous memory locations.
• An array is a collection of data that holds fixed
number of values of same type. .

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


• If you want to store marks of 100 students you can create an array for it.
• float Marks[100];

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Memory representation in an array

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Types of Arrays:
• One Dimensional Arrays
• Two Dimensional Arrays
• Multidimensional Arrays

Note: Dimension refers to the array's size, which is


how big the array is.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


One dimensional array in C
• One-dimensional arrays, also known as
single arrays, are arrays with only one
dimension or a single row.

• A list of items can be given one variable


name using one subscript (index) and such
a variable is called a single-subscripted
variable or a one-dimensional array
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
For Example –
 If we want to represent a set of five numbers say (35, 40, 20, 57, 19), by an
array variable number, then number is declared as → int number [5];
 Computer store (35, 40, 20, 57, 19) as shown below:
The values can be assigned to the array as:
• number [0] = 35;
• number [1] = 40;
• number [2] = 20;
• number [3] = 57;
• number [4] = 19;

 Declaration of One-dimensional array

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Declaration of one-dimensional
array
Syntax
type variable-name[size];
Here the type specifies the data type of elements
contained in the array, such as int, float, or char.
variable-name specifies the name of the array.
[size] represents the maximum number of data
elements an array can store. It can be a numeric
constant or a symbolic constant.
Prepared by:Mrs.G.S.Patil(Assistant ProfessorETC)
• Declare an array to contain the numbers of 5 integer
constants.
int number[5];
• The compiler reserves five storage locations.
The general form of array declaration is:
type array-name[size];
 Here the type specifies the data type of elements contained in the array, such
as int, float, or char.
 The size indicates the maximum numbers of elements that can be stored
inside the array.
For example: int rollno [10] ;
 Here int is datatype, rollno is a variable name, 10 is a size of array and the
subscripts (index) is start from 0 to 9.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Initialization of one-dimensional array
• After declaration an array needs to be
initialized i.e. assign values to data
elements.
• Initialization can be done :
1. At Compile time
2. At run time
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
At Compile time

• Values of data elements can be initialized at the time of


declaration.
Syntax
type array-name[size]={list of values};
The values in the list are separated by commas.
int number[3]={4,7,3};
float total[5]={0.0,15.57,-10};
int c[]={1,2,3,4};//size is omitted.
int number[3]={13,46,365,346};//This is not allowed
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
• Initialization of One-dimensional array
After an array is declared, its elements must be initialized.
In C programming an array can be initialized at either of the following stages:
 At compile time
 At run time

 Compile time initialization


▪ In compile time initialization, the array is initialized when they are declared.
▪ The general form of initialization of array is:
type array-name[size] = { list of values };
The list of values is separated by commas.
Example: int number[3] = {4, 5, 9};
Here declare the variable ‘number’ as an array of size 3 and will assign the values
to each elements.
4 is assign to first element(number[0]),
5 is assign with second element(number[1]),
9 is assign with third element(number[2]).
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
 If the number of values in the list is less than the length (size), then only that
many elements will be initialized.
 The remaining elements will be set to zero automatically.
 If we have more initializers than the declared size, the compiler will produce
an error.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Example of 1D array
• Suppose we want to store roll no of 100 students ,
then we declare variable roll_no as:
int roll_no[100];

i.e.roll_no[0], roll_no[1], roll_no[2], roll_no[3],


………………. roll_no[99]

The subscript begin with number 0.


Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
Q. Program based on One Dimensional Array (Compile Time)

#include<stdio.h>
#include<conio.h>
void main()
{
int roll_no[5]={10,20,30,40,50};
printf("%d",roll_no[0]);
getch();
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. Example for Compile Time Array (Character Data Type)

#include<stdio.h>
#include<conio.h>
void main()
{
char alpha[5]={'X','N','T','U','Z'};
clrscr();
printf("%c",alpha[2]);
getch();
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. Example for Compile Time Array (Float Data Type)

#include<stdio.h>
#include<conio.h>
void main()
{
float perc[5]={37.85,45.67,31.29,51.67,78.23};
clrscr();
printf("%f\t""%f",perc[2],perc[3]);
getch();
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


At Run time :

 An array can also be initialized at runtime using scanf()


function.
 This approach is usually used for initializing large arrays, or to
initialize arrays with user specified values.
 To input elements in an array, we can use a for loop or insert
elements at a specific index.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


At Run time :

• An array can be initialized at run time i.e.after


execution of program.
• Usually applied for initializing large arrays.
for (i=0;i<50;i++)
{
arr[i]=i+1;
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. Program to Display Five Characters using Array datatypes (run time).

#include<stdio.h>
#include<conio.h>
void main()
{
char alpha[5],i;
clrscr();
printf("Enter Array Elements: ");
for (i=0;i<5;i++)
{
scanf ("%c",&alpha[i]);
}
printf ("\n Array Elements: ");
for (i=0;i<5;i++)
{
printf("%c\t",alpha[i]);
}
getch();
}
Q. Program to Display Five Decimal values using Array datatypes (run time).

#include<stdio.h>
#include<conio.h>
void main()
{
float alpha[5],i;
clrscr();
printf("Enter Array Elements: ");
for (i=0;i<5;i++)
{
scanf ("%f",&alpha[i]);
}
printf ("\n Array Elements: ");
for (i=0;i<5;i++)
{
printf("%f\t",alpha[i]);
}
getch();
}
 A variable which represents the list of items using two index (subscript) is
called two-dimensional array.
 Two-dimensional array is known as matrix.
 In Two dimensional arrays, the data is stored in rows and columns format.
 2-d array is a collection of 1-D array placed one below the other.
Its syntax is:
Data-type array name[row][column];

Total no. of elements in 2-D array is calculated as row*column


• Example:
int a[2][3];
Total no of elements= row*column = 2*3 = 6
It means the matrix consist of 2 rows and 3 columns

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


 A two-dimensional array a, which contains 2 rows and 3 columns can be shown as
follows:

For example: int a[2][3] = {20, 2, 7, 8, 3, 15};

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Declaration of Two-dimensional arrays:
 The general form of two-dimensional array declaration is:

type array-name[row_size][column_size];

Here the type specifies the data type of elements contained in the array, such as int, float, or
char.
The size indicates the size of number of rows and number of columns.

Initialization of Two-dimensional arrays:


 The general form of initializing two-dimensional array is :

type array-name[row_size][column_size] = {list of values};

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Example: int table[2][3] = {0, 0, 0, 1, 1, 1};
 Here the elements of first row initializes to zero and the elements of second row initializes to
one.
 This above statement can be written as :
int table[2][3] = {
{0,0,0},
{1,1,1}
};
 In two-dimensional array the row size can be omitted.
Example:
int table[ ][3] = {{0,0,0}, {1,1,1}};

 If the values are missing in an initializer, they are automatically set to zero.
Example:
int table[2][3] = {1,1,2};

 Here first row initializes to 1,1 and 2 and second row initialize to 0,0 and 0 automatically.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Memory layout of Two-dimensional arrays:

In Two dimensional arrays, the data is stored in rows and columns format.
For example:
int table[2][3] = {1,2,3,4,5,6};
The memory layout of above example:
table[0][0] = 1;
table[0][1] = 2;
table[0][2] = 3;
table[1][0] = 4;
table[1][1] = 5;
table[1][2] = 6;

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


TWO-DIMENSIONAL ARRAYS
• To store data in table form 2D Arrays are used.
• For example:
Roll No subject1 subject2 subject3 subject4 subject5
209 100 47 58 39 57
89 75 59 49 59 58
67 89 29 67 9 39
489 67 59 67 58 48
46 4 59 58 79 74
78 98 35 75 57 46

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


• Two-dimensional array has
-Rows

-Columns

• A list of items can be given one variable name using


two subscripts and such a variable is a two-
dimensional array.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Declaration of 2D Array:
• type array-name[row-size][colum-size];
• Example:
int value[4][3];
• first index:-No. of rows
• Second index:-No.of columns

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Representation of 2D array in memory:

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Initialization of 2D arrays:
• int table[2][3]={0,0,0,1,1,1};
• int table[2][3]={{0,0,0},{1,1,1}};
• int table[2][3]={
{0,0,0},
{1,1,1}
};
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
• int table[ ][3]={
{0,0,0},
{1,1,1}
};

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


• int table[2][3]={
{0,0},
{1,}
};

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


• When all the elements need to be initialized to 0 :
• int table[2][3]={{0},{0}}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. 2 Dimensional array program with compile time initialization.(integer type)

#include<stdio.h>
#include<conio.h>
void main()
{
int s[2][3]={
{7,8,9},
{6,5,4}
};
int i,j;
clrscr();
for (i=0;i<=1;i++)
{
printf ("\n");
for(j=0;j<=2;j++)
printf("%d\t",s[1][2]);
}
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. 2 Dimensional array program with compile time initialization.(character type)

#include<stdio.h>
#include<conio.h>
void main()
{
char s[2][3]={
{'A','W','T'},
{'R','V','L'}
};
char i,j;
clrscr();
for (i=0;i<=1;i++)
{
printf ("\n");
for(j=0;j<=2;j++)
printf("%c\t",s[1][1]);
}
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. 2 Dimensional array program with compile time initialization.(float type)

#include<stdio.h>
#include<conio.h>
void main()
{
float s[2][3]={
{1.1,2.5,6.7},
{2.1,6.3,4.1}
};
float i,j;
clrscr();
for (i=0;i<=1;i++)
{
printf ("\n");
for(j=0;j<=2;j++)
printf("%f\t",s[0][1]);
}
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. Program for Declaration of two dimensional array (Run-time)

#include<stdio.h>
#include<conio.h> printf("Two D array elements are\n");
int main() for (i=0;i<2;i++)
{ {
int a[2][3],i,j; for (j=0;j<3;j++)
printf("Enter array element"); {
for(i=0;i<2;i++) printf("%d",a[i][j]);
{ }
for (j=0;j<3;j++) printf("\n");
{ }
printf("\nelement a[%d][%d]= ",i,j); return 0;
scanf("%d",&a[i][j]); }
}
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
Q. Program for Declaration of two dimensional array(Float Data type)

#include<stdio.h>
#include<conio.h> printf("Two D array elements are\n");
void main() for (i=0;i<2;i++)
{ {
float a[2][3],i,j; for (j=0;j<3;j++)
printf("Enter array element"); {
for(i=0;i<2;i++) printf("%f",a[i][j]);
{ }
for (j=0;j<3;j++) printf("\n");
{ }
printf("\nelement a[%f][%f]= ",i,j); getch();
scanf("%f",&a[i][j]); }
}
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
Q. Program for Declaration of two dimensional array(Char Data type)

#include<stdio.h> fflush(stdin);
#include<conio.h> scanf("%c",&a[i][j]);
int main() }
{ }
char a[2][3]; printf("Two D array elements are\n");
int i,j; for (i=0;i<2;i++)
clrscr(); {
printf("Enter array element"); for (j=0;j<3;j++)
for(i=0;i<2;i++) {
{ printf("%c\t",a[i][j]);
for (j=0;j<3;j++) }
{ printf("\n");
printf("\nelement a[%d][%d]= ",i,j); }
getch();
return 0;
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
Multidimensional arrays:
• It has three or more dimensions.
• The exact limit is determined by the compiler.
Syntax
• type array-name[s1][s2]….[sn];

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. Program for Declaration of three dimensional array(int Data type)

#include<stdio.h>
#include<conio.h>
int main()
{
int x,y;
clrscr();
int a[2][2][2]={{{1,2},{3,4}},{{5,6},{7,8}}};
x=a[1][1][0];
y=(*(*(*(a+1)+1)+0));
printf("%d %d",x,y);
return 0;
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
Character Arrays/ Strings:
• It is the sequence of characters.
• A string is actually a one-dimensional array
of characters in C language.
• %s is the format specifier for string.
• Example: “programming” is an example of
String.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


DECLARING STRING :

• Since we cannot declare a string using String Data


Type, instead we use an array of type “char” to
create a String.
• C string is the string is terminated with a unique
character ‘\0’.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


example
• char c[] =“c string”;

c s t r i n g \0

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Declaration of string
• Declaring a string in C is as simple as declaring a one-
dimensional array

• char string_name[size];

• Size=maximum number of characters+1(for \0)

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Initialisation :

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


• char city[9]=“NEW YORK”;
• char city[9]={‘N’,’E’,’W’,’ ‘,’Y’,’O’,’R’,’K’,’\0’};
• char city[ ]={‘N’,’E’,’W’,’ ‘,’Y’,’O’,’R’,’K’,’\0’};
• Char string[10]=“GOOD”;

G O O D \0 \0 \0 \0 \0 \0

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


• char str[3]=“GOOD”;
• char str3[5];
str3=“GOOD”;

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. Example for string displaying the name.(Compile-Time)

#include<stdio.h>
int main ()
{
char name[30]="Rohit";
printf("%s",name);
}

Q. Example of String displaying the name. (Run-Time)

#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%s",name);
printf("%s",name);
}
Q. Example of String displaying the name. (Run-Time)

#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
gets(name);
printf("%s",name);
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Read String from the user
scanf()

• You can use the scanf() function to read a string.


• %s is the format specifier to read in a string.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


%ws-format specifier:
• %ws in scanf () for reading a specified number of
characters from input string.
• w (width) is equal to or greater than No. of characters
typed in.
• If w is less then No.of characters typed in,then excess
characters are truncated.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Example

• char a[10];
• scan(“%5s”,a);
If input is RAM, then string stored is as follows:
R A M \0 \0 \0 \0 \0 \0 \0
• If input is KRISHNA,then string stored will be
K R I S H \0 \0 \0 \0 \0

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. Example for %ws format

#include<stdio.h>
#include<conio.h>
int main()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%3s",name);
printf("%s",name);
return 0;
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Writing string on output window
printf();
• %w.ps in printf() to print first p characters in width w.
• printf(“%10.4s”,name);.......right justified
• printf(%-10.4s”,name);........left justified

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Example:
• NEW DELHI 110001(TOTAL 16 CHARACTERS)
• %20s
N E W D E L H I 1 1 0 0 0 1

 %20.10s
N E W D E L H I

• %-20.10s

N E W D E L H I
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
Q. Example based on %ws in printf

#include<stdio.h>
#include<conio.h>
int main()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%s",name);
printf("%-10.5s",name);
return 0;
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Q. Example based on %ws in printf

#include<stdio.h>
#include<conio.h>
int main()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%s",name);
printf("%10.5s",name);
return 0;
}

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


/ C program to read string from user

Q. Example of String displaying the name. (Run-Time)

#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
scanf("%s",name);
printf("%s",name);
}
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
• The scanf() function reads the sequence of characters
until it encounters space .

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


gets() and puts() functions

• The gets() and puts() are declared in the header file


stdio.h.
• Both the functions are involved in the input/output
operations of the strings.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


gets() function

• It is used to input space-separated strings from the


user.

• Syntax:
gets(stringname);

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


/ C program to read string from user

Q. Example of String displaying the name. (Run-Time)

#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
gets(name);
printf("%s",name);
}
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
puts() function

• The puts() function is very much similar to printf()


function.
• The puts() function is used to print the string on the
screen which is previously read by using gets() or
scanf() function.
• Syntax:
puts(stringname);

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


/ C program to read string from user

Q. Example of String displaying the name. (Run-Time)

#include<stdio.h>
#include<conio.h>
int main ()
{
char name[30];
clrscr();
printf("enter name:");
gets(name);
puts(name);
puts(name);
}
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
String Handling Functions in C:
• C library supports a large number of string handling
functions that can be used to manipulate(perform
different operations) strings.
• string handling functions are defined under "string.h"
header file.

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


Function Work of Function

strlen() computes string's length

strcpy() copies a string to another

strcat() concatenates(joins) two strings

strcmp() compares two strings

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


strlen()
It counts and returns No.of Characters in a string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
int count=0;
char name[30];
clrscr();
printf("Enter name: ");
gets(name);
count=strlen(name);
puts(name);
printf("string length is: %d",count);
return 0;
} Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
strlen()
It counts and returns No.of Characters in a string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int count=0,i=0;
char name[30];
clrscr();
printf("Enter name: "); puts(name);
gets(name); printf(“Length of string is:%d",count);
}
while (name[i]!='\0')
{
count++;
i++;
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
}
strcpy()

• It acts as string assignment operator.

• strcpy(string1,string2);

• Contents of string2 will be assigned to string1

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char stud_name[30],emp_name[30];
printf("Enter Name: ");
gets(stud_name);
strcpy(emp_name,stud_name);
printf("Student Name=%s \n Employee Name=%s", stud_name,
emp_name);
getch();
}
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
#include<stdio.h>
#include <string.h>
int main()
{
char ch[20]={‘H', ‘e', ‘l', ‘l', ‘o', ’ ’, '\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of second string is: %s",ch2);
return 0;
}
Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
strcat()

• The strcat( ) function joins two strings and result is returned to first_string.

• strcat(string1,string2);

#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[30]="Krishna";
char s2[6]=" Patil";
clrscr();
strcat (s1,s2);
printf (“String after cancatenation is: %s\n",s1);
puts(s2); Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
}
strcmp()

• the strcmp( ) function compares two strings and


returns 0 if both strings are equal.
• strcmp(string1,string2);

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)


#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int value;
char s1[30],s2[30];
clrscr();
printf("enter string 1: ");
gets (s1);
printf("enter string 2: ");
gets (s2);
value=strcmp(s1,s2);
if (value==0)
printf("same");
else
printf("not same");
getch();
} Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)
ASCII values-American Standard Code for
Information Interchange

Prepared by Mrs G.S.Patil


END

Prepared by:Mrs.G.S.Patil(Assistant Professor ETC)

You might also like