0% found this document useful (0 votes)
22 views37 pages

Unit - 3

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)
22 views37 pages

Unit - 3

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/ 37

UNIT – 3

Array & string

1
Array
• Array is defined as the collection of similar type of data items
stored at contiguous memory locations. that are referenced by a
common name.

•  For example, if we want to store the marks of a student in 5


subjects, then we don't need to define different variables for the
marks in the different subject.

• Instead of that, we can define an array which can store the marks
in each subject at the contiguous memory locations.
2
Advantage of Array

 Code Optimization: Less code to the access the data.

 Ease of traversing: By using the for loop, we can


retrieve the elements of an array easily.

 Random Access: We can access any element randomly


using the array.

3
Disadvantage of C Array

 Fixed Size:
Whatever size, we define at the time of declaration
of the array, we can't exceed the limit.

4
Types
One-Dimensional array(1-D)

Two-Dimensional array(2-D)

5
How to declare an array

• Syntax:
datatype array_name[size];

Example: int x[3];


x
X[0]
X[1]
X[2]
Array initialization

• Syntax:
data_type array_name[size]={values};

Example: int x[3]={5,3,7};


x
5 X[0]
3 X[1]
7 X[2]

7
Example

 Program to set values of array and display it

 Program to get values of array from users and display it

8
#include<stdio.h>
void main()
{
int a[5]={10,20,30,40,50},i;
clrscr();
for(i=0;i<5;i++)
{
printf("\nThe value in a[%d] is %d",i,a[i]);
}
getch();
}
OUTPUT

The value in a[0] is 10


The value in a[1] is 20
The value in a[2] is 30
The value in a[3] is 40
The value in a[4] is 50
9
#include<stdio.h> OUTPUT
#include<conio.h> Enter five values :
void main() 10
{ 20
int a[5],i;
30
clrscr();
40
printf("Enter five values : \n");
50
for(i=0;i<5;i++)
The value in a[0] is 10
{
scanf("%d",&a[i]); The value in a[1] is 20
} The value in a[2] is 30
for(i=0;i<5;i++) The value in a[3] is 40
{ The value in a[4] is 50
printf("\nThe value in a[%d] is %d",i,a[i]);
}
getch();
}
10
//program to find sum of elements in an array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,sum=0;
clrscr();
printf("Enter five values : \n");
for(i=0;i<5;i++) OUTPUT
{ Enter five values :
scanf("%d",&a[i]); 1
} 2
for(i=0;i<5;i++) 3
{ 4
sum=sum+a[i]; 5
} The sum of elements in array is : 15
printf(“The sum of elements in array is : %d”,sum);
getch();
}
11
//program to sort number in ascending order
#include<stdio.h>
#include<conio.h> }
void main() }
printf(“Ascending order : \n”);
{ for(i=0;i<5;i++)
int a[5],i,j,t; {
clrscr(); printf(“%d\t”,a[i]);
printf("Enter five values : \n"); }
for(i=0;i<5;i++) getch();
}
{
scanf("%d",&a[i]);
} OUTPUT
for(i=0;i<5;i++) Enter five values :
{ 40
for(j=i+1;j<5;j++) 10
{ 30
if(a[i]>a[j]) 50
{
20
t=a[i];
a[i]=a[j]; Ascending order :
a[j]=t; 10 20 30 40 50
} 12
//program to find maximum no in an array
#include<stdio.h>
#include<conio.h>
void main() printf(“Maximum no is %d“,max);
{ getch();
}
int a[5],i,max;
clrscr();
OUTPUT
printf("Enter five values : \n"); Enter five values :
for(i=0;i<5;i++) 10
{ 20
scanf("%d",&a[i]); 30
} 40
max=a[0]; 50
for(i=1;i<5;i++) Maximum no is 50
{
if(max<a[i])
{
max=a[i];
}
} 13
//program to search element in array(Linear Search)
#include<stdio.h>
#include<conio.h> else
void main() {
printf(“Value not found”);
{ }
int a[5]={10,20,30,40,50}; }
int key; getch();
clrscr(); }
printf("Enter search value:\n");
scanf("%d",&key);
for(i=0;i<5;i++) OUTPUT
{
if(key==a[i])
Enter search value : 40
{ Value found
printf(“Value found”);
break;
}

14
Two-Dimensional array
Array Declaration
• Syntax:
data_type array_name[row_size] [col_size];
Example: int x[3][3];

Col 0 Col 1 Col 2

row 0 X[0][0] X[0][1] X[0][2]

row 1 X[1][0] X[1][1] X[1][2]

row 2 X[2][0] X[2][1] X[2][2]


15
Array Initialization
• Syntax:
data_type array_name[row_size] [col_size];={variables};
Example: int x[3][3]={1,50,2,75,8,4,9,33,77};

Col 0 Col 1 Col 2

row 0 1 50 2

row 1 75 8 4

row 2 9 33 77

16
//program to assign values to array and to display it
#include<stdio.h>
#include<conio.h>
void main() OUTPUT
{ Values in array :
int a[3][3]={10,20,30,40,50,60,70,80,90}; a[0][0]=10
int i,j; a[0][1]=20
clrscr(); a[0][2]=30
printf(“Values in array : \n"); a[1][0]=40
for(i=0;i<3;i++) a[1][1]=50
{ a[1][2]=60
for(j=0;j<3;j++) a[2][0]=70
{ a[2][1]=80
printf(“a[%d][%d] = %d\n”,i,j,a[i][j]); a[2][2]=90
}
}
getch();
}

17
//program to assign values to array from user and to display it
#include<stdio.h>
#include<conio.h>
OUTPUT
void main()
Enter 9 values in array :
{
10
int a[3][3],i,j;
20
clrscr();
30
printf(“Enter 9 values in array : \n");
40
for(i=0;i<3;i++)
50
{
60
for(j=0;j<3;j++)
70
{
80
scanf(“%d”,&a[i][j]);
90
}
Values in array :
}
a[0][0]=10
printf(“Values in array : \n”);
a[0][1]=20
for(i=0;i<3;i++)
a[0][2]=30
{
a[1][0]=40
for(j=0;j<3;j++)
a[1][1]=50
{
a[1][2]=60
printf(“a[%d][%d] = %d\n”,i,j,a[i][j]);
a[2][0]=70
}
a[2][1]=80
} getch(); a[2][2]=90 18
}
//program to implement Matrix addition
#include<stdio.h> for(i=0;i<3;i++)
#include<conio.h> {
void main() for(j=0;j<3;j++)
{ {
int a[3][3],b[3][3],c[3][3],i,j;
c[i][j]=a[i][j]+b[i][j];
clrscr();
printf(“Enter values of Matrix A : \n"); }
for(i=0;i<3;i++) }
{ printf(“Added Matrix\n”);
for(j=0;j<3;j++) for(i=0;i<3;i++)
{ {
for(j=0;j<3;j++)
scanf(“%d”,&a[i][j]); {
} printf(“%d\t”,c[i][j]);
} }
printf(“Enter values of Matrix B : \n"); printf(“\n”);
for(i=0;i<3;i++) }
{ getch();
for(j=0;j<3;j++) }
{
scanf(“%d”,&b[i][j]);
}
}

19
//program to implement Matrix addition
OUTPUT
Enter values of Matix A :
1
2
3
4
5
6
7 OUTPUT
8 Addded Matrix
9 2 4 6
Enter values of Matix B : 8 10 12
1 14 16 18
2
3
4
5
6
7
8
9
20
//program to implement Matrix multiplication
#include<stdio.h> for(i=0;i<3;i++)
#include<conio.h> {
void main() for(j=0;j<3;j++)
{ {
int a[3][3],b[3][3],c[3][3],i,j,k;
c[i][j]=0;
clrscr();
printf(“Enter values of Matix A : \n"); for(k=0;k<3;k++)
for(i=0;i<3;i++) {
{ c[i][j]=c[i][j]+a[i][k]*b[k][j];
for(j=0;j<3;j++) }
{ }
scanf(“%d”,&a[i][j]); }
} printf(“Multiplied Matrix\n”);
} for(i=0;i<3;i++)
printf(“Enter values of Matix B : \n"); {
for(i=0;i<3;i++) for(j=0;j<3;j++)
{ {
for(j=0;j<3;j++) printf(“%d\t”,c[i][j]);
{
}
printf(“\n”);
scanf(“%d”,&b[i][j]); }
} getch();
} }

21
//program to implement Matrix
OUTPUT
Enter values of Matrix A :
1
1
1
1
1
1
1 OUTPUT
1 Multiplied Matrix
1 1 1 1
Enter values of Matrix B : 1 1 1
1 1 1 1
1
1
1
1
1
1
1
1
22
//program to find transpose of Matrix
#include<stdio.h> for(i=0;i<3;i++)
#include<conio.h> {
void main() for(j=0;j<3;j++)
{ {
printf(“%d\t”,i,j,a[j][i]);
int a[3][3],i,j; } printf(“\n”);
clrscr(); }
printf(“Enter values in array : \n"); getch();
for(i=0;i<3;i++) } OUTPUT
{ Enter values in array :
for(j=0;j<3;j++) 1
{ 2
scanf(“%d”,&a[i][j]); 3
} 4
} 5
printf(“\nTranspose of given Matrix : \n”); 6
7
8
9
Transpose of given Matrix :
1 4 7
2 5 8
3 6 9 23
string
STRING
String is a sequence of characters enclosed
within double quotes.

Ex:”Hello World“
String Declaration

 There are two ways to declare strings in C:

 Method:1
create a string as "Scaler" where the last character must always be a
null character.

Ex: char company[7] = {'S', 'C', 'A', 'L', 'E', 'R' , '\0'};
Method:2
In this method, we do not need to put the null
character at the end of the string constant. The compiler
automatically inserts the null character at the end of the
string.

EX:char company[] = "SCALER";


String Functions
• strlen()
It is used to find the length of the string.
syntax:
strlen(string)
• strcpy()
It is used to copy one string to another.
syntax:
strcpy(string1,string2)
• strcat()
It is used to combine two strings.
syntax:
strcat(string1,string2)

28
• strcmp()

It is used to compare two strings.

syntax:
strcmp(string1,string2)
– Returns 0 if two strings are equal.

• strrev()

It used to reverse a string.


syntax:
strrev(string)
• strlwr(), strupr()

It used to change the case of a string.

syntax:

strlwr(string)
strupr(string)
29
strlen()
It is used to find the length of the string.
syntax:
strlen(string)

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[]="college";
int b;
clrscr();
b=strlen(a);
printf("\nThe length of the string is %d",b);
getch();
}

Output:
The length of the string is 7 30
strcpy()
It is used to copy one string to another.
syntax:
strcpy(string1,string2)

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[]=“Cse";
char b[]="Dept";
clrscr();
strcpy(a,b);
printf("\nThe string is %s",a);
getch();
}

Output:
The string is Dept
31
strcat()
It is used to combine two strings.
syntax:
strcat(string1,string2);

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[]=“Cse";
char b[]="Dept";
clrscr();
strcat(a,b);
printf("\nThe string is %s",a);
getch();
}

Output:
The string is CseDept
32
strcmp()
It is used to compare two strings.
syntax:
strcmp(string1,string2)
Returns 0 if two strings are equal.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10]=“csedept"; Output:
char b[10]=“cse"; Strings are not equal
int i;
clrscr();
i=strcmp(a,b);
if(i==0)
printf(“Strings are equal”);
else
printf("Strings are not equal”);
getch();
} 33
strrev()
It used to reverse a string.
syntax:
strrev(string);

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[]="Dept";
clrscr();
printf("\nThe reversed string is %s",strrev(a));
getch();
}

Output:
The string is tpeD
34
strlwr(), strupr()
It used to change the case of a string.
syntax: strlwr(string);
strupr(string);

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20]=“csedept";
clrscr();
printf("\nThe string is :%s",a);
strupr(a);
printf("\nThe string after conversion to uppercase :%s",a);
strlwr(a);
printf("\nThe string after conversion to lowercase :%s",a);
getch();
} Output
The string is :csedept
The string after conversion to uppercase :CSEDEPT
The string after conversion to lowercase :csedept
35
String Palindrome
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[15],s2[15];
printf("\nenter the string:");
scanf("%s",s1);
strcpy(s2,s1);
strrev(s1);

36
if(strcmp(s1,s2)==0)
printf("\n The string is palindrome");
else
printf("\n The string is not a palindrome");
getch();
}

Output:
enter the string: aba

The string is palindrome

37

You might also like