You are on page 1of 38

UNIT - 3

1
Array
• It is a collection of elements of same data
type, that are referenced by a common
name

• Types
– One-Dimensional array
– Two-Dimensional array
– Multi-Dimensional array

2
One-Dimensional array
Array Declaration
• Syntax:
datatype array_name[size];
Example: int x[3];

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

• Syntax:
data_type array_name[size]={variables};
Example: int x[3]={5,3,7};

int a[5];
x a[0]=10;
X[0] a[1]=20;
5 a[2]=30;
a[3]=40;
3 X[1] a[4]=50;
7 X[2] Int a[5]={10,20,30,40,50};
char a[6]=“Nafees”;

4
//program to set values of array and display it
#include<stdio.h>
OUTPUT
#include<conio.h>
void main()
The value in a[0] is 10
{
The value in a[1] is 20
int a[5],i;
The value in a[2] is 30
clrscr();
The value in a[3] is 40
a[0]=10;
The value in a[4] is 50
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
for(i=0;i<5;i++)
{
printf("\nThe value in a[%d] is %d",i,a[i]);
}
getch();
}
5
//program to get values of array from users and display it
#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
{ The value in a[1] is 20
scanf("%d",&a[i]); The value in a[2] is 30
} The value in a[3] is 40
for(i=0;i<5;i++) The value in a[4] is 50
{
printf("\nThe value in a[%d] is %d",i,a[i]);
}
getch();
}
6
//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];
for(i=1;i<5;i++) 50
{ Maximum no is 50
if(max<a[i])
{
max=a[i];
}
}
7
//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();
}
8
//program to search element in array(linear search)
#include<stdio.h>
#include<conio.h> else
{
void main() printf(“Value not found”);
{ break;
int i,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
{ Enter search value : 40
if(key==a[i]) Value found
{
printf(“Value found”);
break;
}

9
//program to sort number in ascending order
#include<stdio.h>
#include<conio.h> }
void main() }
{ printf(“Ascending order : \n”);
int a[5],i,j,t; for(i=0;i<5;i++)
{
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 :
{
for(j=i+1;j<5;j++) 40
{ 10
if(a[i]>a[j]) 30
{ 50
t=a[i]; 20
a[i]=a[j];
Ascending order :
a[j]=t;
} 10 20 30 40 50
10
Two-Dimensional array
Array Declaration
• Syntax:
data_type array_name[row_size] [col_size];
Example: int x[3][2];
Col 0 Col 1 Col 2

X[0][0] X[0][1]
row 0 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]


11
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

12
//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();
}

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

15
//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
16
//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; {
clrscr(); c[i][j]=0;
printf(“Enter values of Matix A : \n"); for(k=0;k<m;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”);
printf(“Enter values of Matix B : \n"); for(i=0;i<3;i++)
for(i=0;i<3;i++) {
{ for(j=0;j<3;j++)
for(j=0;j<3;j++) {
{ printf(“%d\t”,c[i][j]);
scanf(“%d”,&b[i][j]); }
} printf(“\n”);
} }
getch();
}
17
//program to implement Matrix
OUTPUT
Enter values of Matrix A :
1
1
1
1
1
1
1 OUTPUT
1 Multiplied Matrix
1 3 3 3
Enter values of Matrix B : 3 3 3
1 3 3 3
1
1
1
1
1
1
1
1
18
//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\n”,i,j,a[j][i]);
int a[3][3],i,j; }
clrscr(); }
printf(“Enter values in array : \n"); getch();
} OUTPUT
for(i=0;i<3;i++)
{ Enter values in array :
for(j=0;j<3;j++) 1
2
{
3
scanf(“%d”,&a[i][j]); 4
} 5
} 6
printf(“\nTranspose of given Matrix : \n”); 7
8
9
Transpose of given Matrix :
1 4 7
2 5 8
3 6 9 19
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)
20
• 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)

21
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 22
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 23
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
24
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[]=“csedept"; Output:
char b[]=“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();
25
}
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
26
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[]=“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
27
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);

28
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

29
/*C program to compare the string without using lib.function*/
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20], str2[20];
int i=0, c=0;
printf("Enter the 1st string:\n");
gets(str1);
printf("Enter the 2nd string:\n");
gets(str2);
while((str1[i]!='\0') || (str2[i]!='\0'))
{
if(str1[i]!=str2[i])
c++;
i++;
}
if(c==0)
puts("Strings are equal");
else
puts("Strings are not equal");
getch();
}
Output:
Enter the 1st string:
India
Enter the 2nd string:
India
Strings are equal 30
/*C program to reverse the string without using lib.function*/
#include<stdio.h>
#include<string.h>
int main()
{
char str[100], temp;
int i, j = 0;
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("\nReverse string is :%s", str);
return (0);
}
Output:
Enter the string : Pritesh
Reverse string is : hsetirP

31
/*C program to copy the string without using lib.function*/
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
scanf("%s",s1);
for(i = 0; s1[i] != '\0'; i++)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf("String s2: %s", s2);
getch();
}
Enter String s1:
programiz
String s2:programiz

32
/*C program to concatenate the two strings without using lib.function*/
#include <string.h>
#include <conio.h>
#include <stdio.h>
void main()
{
int i,len=0,j;
char str[50],str1[50],ch,ch1;
printf("\nEnter the First String : " );
gets(str);
printf("\nEnter the Second String : " );
gets(str1);
for(i=0; str[i]!='\0'; i++)
{
len++;
}
len=len-1;
for(i=0; str1[i]!='\0'; i++)
{
str[len++]=str1[i];
}
str[len]='\0';
printf("\n Concatenated String is: %s ",str);
getch();
}
Output:
Enter the First String : computer
Enter the Second String : programming
Concatenated String is: computerprogramming

33
/*C program to calculate the length of the string without using lib.function*/
#include<stdio.h>
#include<conio.h>
void main()
{
char string[50];
int i, length = 0;
printf("Enter a string \n");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("So, the length of %s = %d\n", string, length);
getch();
}
Output:
Enter a string
computer
The length of computer = 8

34
/*C program to count the number of vowels in a string.*/
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int vowels = 0, i = 0;
clrscr();
printf(“ENTER A STRING:\n “);
gets(str);
while(str[i] != ‘\0′)
{
if(str[i]==’A’ || str[i]==’a’ || str[i]==’E’ || str[i]==’e’ || str[i]==’I’ || str[i]==’i’ ||
str[i]==’O’ || str[i]==’o’ || str[i]==’U’ || str[i]==’u’)
vowels++;
i++;
}
printf(“THE TOTAL NUMBER OF VOWELS IS: %d”, vowels);
getch();
}
output:
ENTER A STRING:
computer
THE TOTAL NUMBER OF VOWELS IS :3

35
/*C program to count digits, spaces, special characters, alphabets in a string.*/
#include <stdio.h>
#include<conio.h>
void main()
{
char str[100];
int countDigits,countAlphabet,countSpecialChar,countSpaces;
int counter;
countDigits=countAlphabet=countSpecialChar=countSpaces=0;
printf("Enter a string: ");
gets(str);
for(counter=0;str[counter]!=NULL;counter++)
{
if(str[counter]>='0' && str[counter]<='9')
countDigits++;
else if((str[counter]>='A' && str[counter]<='Z')||(str[counter]>='a' && str[counter]<='z'))
countAlphabet++;
else if(str[counter]==' ')
countSpaces++;
else
countSpecialChar++;
}
printf("\nDigits: %d \nAlphabets: %d \nSpaces: %d \nSpecial Characters:
%d",countDigits,countAlphabet,countSpaces,countSpecialChar);
getch();
}
Output:
Enter a string: This is test string, 123 @#% 98. Digits: 5 Alphabets: 16 Spaces: 6 Special Characters: 5
36
/* C Program – Find the occurrences of Character in String */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i, count=0;
char str[1000], ch;
printf("Enter the String : ");
gets(str);
printf("Enter a character to find the occurrences : ");
scanf("%c",&ch);
for(i=0; str[i]!='\0'; i++)
{
if(ch==str[i])
{
count++;
}
}
printf(“occurences of the character %c = %d",ch, count);
getch();
}
Output:
Enter the String:computer
Enter a character to find the occurrences :c
Occurences of the character c=1
37
/* C Program - Count Occurrence of Words in Sentence */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char strs[100], countw=0, strw[15], i;
printf(“Enter the sentence : ");
gets(strs);
int len=strlen(strs);
for(i=0; i<len; i++)
{
if(strs[i]==' ')
{
countw++;
}
}
printf("Total number of words in the sentence is %d",countw+1);
getch();
}
Output:
Enter the sentence: hello world
Total number of words in the sentence is 2

38

You might also like