Unit 3
Unit 3
Venkateshwalru 1/19
Programming in C
Unit-3
Topic: Arrays and Strings
by
Dr. K. Venkateshwalru,
Assistant Professor,
Anurag University.
Dr. K. Venkateshwalru 2/19
Arrays:
Introduction to Arrays, One-Dimensional Arrays and Multi-Dimensiona
Arrays with suitable illustrative programs.
Strings:
Introduction to Strings, Unformatted I/O ,String Operations
with and without builtin functions. (strlen( ), strcmp( ), strcat(
),strcpy( ), and strrev( )) with suitable illustrative programs.
Dr. K. Venkateshwalru 3/19
What is an Array?
What is an Array?
Array
Declaration of Array:
Example: int scores[10] = {23, 45, 12, 67, 95, 45, 56, 34, 83, 16};
Dr. K. Venkateshwalru 9/19
Initialization of array
#include<stdio.h>
int main()
{
int a[4]={1,2,3,4};
int b[4];
b=a;// copying not allowed
for(int i=0; i<4; i++)
printf("%d\t",a[i]);
}
Dr. K. Venkateshwalru 9/19
#include<stdio.h>
int main()
{
int i,a[4]={1,2,3,4};
int b[4];
// Copying array
for(i=0; i<4; i++)
b[i]=a[i];
// displaying
printf("\n b=[ ");
for( i=0; i<4; i++)
printf("%d\t",b[i]);
printf("]");
}
Dr. K. Venkateshwalru 9/19
Out put:
b=[ 1 2 3 4 ]
Dr. K. Venkateshwalru 10/19
1-D array
int main()
{
int arr[5];
arr[0] = 5;
arr[2] = -10;
arr[3 / 2] = 2; // this is same as arr[1] = 2
arr[3] = arr[0];
printf("%d %d %d %d", arr[0], arr[1], arr[2], arr[3]);
return 0;
}
Out put:
5 2 -10 5
Dr. K. Venkateshwalru 10/19
1-D array
#include <stdio.h>
int main()
{
int arr[2]={10, 20};
printf("%d ", arr[1]);
printf("%d ", arr[2]);
printf("%d ", arr[3]);
printf("%d ", arr[-1]);
return 0;
}
Out put
20 1 0 0
Dr. K. Venkateshwalru 10/19
1-D array
#include<stdio.h>
int main()
{
int i=0;
int marks[5]={20, 30, 40, 50, 60};
for (i=0;i<5;i++)
printf("%d\n", marks[i]);
return 0;
}
Dr. K. Venkateshwalru 10/19
1-D array
1-D array
printf("]");
for(i=0;i<n;i++) // calculating sum
sum=sum+a[i];
avg=(float)sum/n;
printf("\n sum=%d \n Average=%f",sum, avg);
min=a[0];
max=a[0];
for(i=1;i<n;i++)
{
if(a[i]<min)
min=a[i];
if(a[i]>max)
max=a[i];
}
printf("\n minimum=%d\n Maximum=%d",min, max);
return 0;
}
Dr. K. Venkateshwalru 10/19
1-D array
Output:
Enter a[0]=8
Enter a[1]=6
Enter a[2]=5
Enter a[3]=4
Enter a[4]=2
The array elements are [ 8 6 5 4 2 ]
sum=25
Average=5.000000
minimum=2
Maximum=8
Dr. K. Venkateshwalru 10/19
1-D array
#include<stdio.h>
int main()
{
int a[30],i,flag=0,n,key;
printf("Enter no.of elements\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
printf("\n Enter a[%d]=",i);
scanf("%d",&a[i]);
}
printf("Enter key element\n");
scanf("%d",&key);
for(i=0;i<n;i++)
Dr. K. Venkateshwalru 10/19
1-D array
{
if(key==a[i])
{
flag=1;
break;
}
}
if(flag==1)
{
printf("\n The Element is found at position %d\n ",i+1);
printf("\n Search is Successful\n");
}
else
printf("The Element is not found \nSearch is Unsuccessful\
return 0;
}
Dr. K. Venkateshwalru 10/19
1-D array
Out put:
Enter no.of elements 5
Enter 5 elements
Enter a[0]=7
Enter a[1]=8
Enter a[2]=5
Enter a[3]=4
Enter a[4]=3
1-D array
1-D array
Out put:
Enter the size of an arry:5
Array elements=
2 4 6 8 10
Dr. K. Venkateshwalru 11/19
Multi-Dimensional Arrays
The declarations
int arr[2][3]={10, 20, 30, 40, 50, 60};
int arr[ ][3]={10, 20, 30, 40, 50, 60};
are prefectly acceptable.
Where as,
int arr[2][ ]={10, 20, 30, 40, 50, 60};
int arr[ ][ ]={10, 20, 30, 40, 50, 60}
would never work.
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
#include<stdio.h>
int main()
{ int r, c, i, j, a[10][10];
printf("\n Enter no. of rows");
scanf("%d",&r);
printf("\n Enter no. of columns");
scanf("%d",&c);
// Reading 2D array elements
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("a[%d][%d]=",i+1,j+1);
scanf("%d",&a[i][j]);
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
}
}
// display 2D array elements
printf("\n A=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
}
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
Output:
Enter no. of rows 3
Enter no. of columns 3
a[1][1]=1
a[1][2]=2
a[1][3]=3
a[2][1]=4
a[2][2]=5
a[2][3]=6
a[3][1]=7
a[3][2]=8
a[3][3]=9
A=
1 2 3
4 5 6
7 8 9
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
printf("\n =====Matrix Addition=====\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d\t",c[i][j]);
} printf("\n");
}
}//end of if
else
printf("\n Addition of matrices is not possible");
}// end of main
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
Out put: 1
----------------------
Input rows and columns of A Matrix: 2 2
Input rows and columns of B Matrix: 2 2
Enter 4 elements of matrix A:
1 2
3 4
Enter 4 elements of matrix B:
2 2
3 4
=====Matrix Addition=====
3 4
6 8
--------------------------------
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
Out put: 2
}
}
// display 2D array elements
printf("\n A=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
if (i>j)
{
if(a[i][j]!=0)
flag=1;
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
}
}
if (flag==0)
printf("\n Matrix A is an Upper Triangle Matrix");
else
printf("\n Matrix A is not an Upper Triangle Matrix");
scanf("%d",&a[i][j]);
// Reading elements of matrix B
printf("Enter %d elements of matrix B:\n", r2*c2);
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
printf("\n =====Matrix Multiplication Result==\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<r2;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
} // end of i-loop
// displaying result matrix
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d\t", c[i][j]);
printf("\n");
}
} // end of if
else
printf("\n Matrix multiplication is not possible");
}
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
Out Put: 1
---------------------------------
Input rows and columns of A Matrix:2 2
Input rows and columns of B Matrix:2 2
Enter 4 elements of matrix A:
1 2
3 4
Enter 4 elements of matrix B:
2 2
1 1
=====Matrix Multiplication=====
4 4
10 10
-----------------------------------------------
Out Put: 2
Input rows and columns of A Matrix:2 3
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
int main()
{
int n, i, j, temp;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the %d elements of array :",n);
int a[n]; //Array Declaration
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++) //Sorting Array in descending orde
for(j=i+1; j<n ;j++)
{
if(a[i]<a[j])
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
Out put:
Sorted array
25 12 10 5 4
The second smallest element is 5
int main()
{
int n, i, j, temp;
printf("Enter the number of elements:");
scanf("%d",&n);
printf("Enter the %d elements of array :",n);
float a[n]; //Array Declaration
for(i=0;i<n;i++)
scanf("%f",&a[i]);
for(i=0;i<n;i++) //Sorting Array in descending order
for(j=i+1; j<n ;j++)
{
if(a[i]<a[j])
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
Out put:
Enter the number of elements:5
Enter the 5 elements of array :1 2 3 4 5
Enter a number:123
sum=sum+r;
if(ld<r)
ld=r;
}
printf("\n Largest digit=%d",ld);
printf("\n sum of digits of %d =%d", n, sum);
}
else
printf("\n your not entered 4-digit no");
}
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
out put:
Largest digit=7
sum of digits of 4571 =17
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
{
count=1;
for(j=i+1;j<size;j++)
if(arr[i]==arr[j])
{
count++;
freq[j]=0;
}
if(freq[i]!=0)
freq[i]=count;
}
printf("\n Frequency of all elements of array : \n");
for(i=0; i<size; i++)
if(freq[i]!=0)
printf("%d occurs %d times\n", arr[i], freq[i]);
}
Dr. K. Venkateshwalru 13/19
Multi-Dimensional Arrays
Two-dimensional array
Advantages of an Array in C:
1 Random access of elements using the array index.
2 Use of fewer lines of code as it creates a single array of multiple
elements.
3 Easy access to all the elements.
4 Traversal through the array becomes easy using a single loop.
5 Sorting becomes easy as it can be accomplished by writing fewer
lines of code.
Disadvantages of an Array in C:
1 Allows a fixed number of elements to be entered which is de-
cided at the time of declaration. Unlike a linked list, an array
in C is not dynamic.
2 Insertion and deletion of elements can be costly since the ele-
ments are needed to be managed in accordance with the new
memory allocation
Dr. K. Venkateshwalru 15/19
Strings in C
Strings
Declaration of Strings
Initializing a String
#include <stdio.h>
int main()
{
char data[]={’H’,’E’,’L’,’L’,’O’,’\0’};
printf("string is :%s\n",data);
printf("%c",data[0]);
return 0;
}
// OUT PUT
string is : HELLO
H
Dr. K. Venkateshwalru 18/19
Strings in C
#include <stdio.h>
int main()
{
char data1[20]={’H’,’E’,’L’,’L’,’O’,’\0’};
char data2[]={’H’,’E’,’L’,’L’,’O’,’\0’};
char data3[20]="HELLO";
char data4[]="HELLO";
printf("data1 is:%s\n",data1);
printf("data2 is:%s\n",data2);
printf("data3 is:%s\n",data3);
printf("data4 is:%s\n",data4);
return 0;
}
Dr. K. Venkateshwalru 18/19
Strings in C
// out put
data1 is:HELLO
data2 is:HELLO
data3 is:HELLO
data4 is:HELLO
Dr. K. Venkateshwalru 18/19
Strings in C
#include <stdio.h>
int main()
{
char data[10];
printf(" Enter String:");
for(int i=0;i<10;i++)
scanf("%c", &data[i]);
printf(" Entered String is:");
printf("%s",data);
return 0;
}
// Out put
Enter String:Anurag University
Entered String is:Anurag Uni
Dr. K. Venkateshwalru 18/19
Strings in C
#include <stdio.h>
int main()
{
char data[]="Hello World";
for(int i=0;data[i]!=’\0’;i++)
printf("%c",data[i]); // printing string as characters
printf("\n%s",data); // printing string as string
return 0;
}
// Out Put
Hello World
Hello World
Dr. K. Venkateshwalru 18/19
Strings in C
// out put
Enter a String
Anurag University
The string you entered is :
Dr. K. Venkateshwalru 18/19
Strings in C
Anurag University
Dr. K. Venkateshwalru 19/19
Strings in C
#include <stdio.h>
#include<string.h>
int main()
{
char str1[]="Anurag";
char str2[]="Anurag";
char str3[]="University";
printf("\n str1=%s\n",str1);//printing each string
printf("\n str2=%s\n",str2);
printf("\n str3=%s\n",str3);
int c;
c=strcmp(str2, str1);
printf("\n Strcmp(str2, str1)=%d\n",c);
int length;
Dr. K. Venkateshwalru 19/19
Strings in C
length=strlen(str1);
printf("\n The length of the str1 is:%d\n",length);
strcpy(str2, str3);
printf("\n After strcpy(str2, str3), str2=\n");
puts(str2);
strcat(str1, str3);
printf("\n After strcat(str1, str3), str1=\n");
puts(str1);
printf("\n");
printf("\n str3=%s\n",str3);
strrev(str3);// reverse of a string
printf("\n After strrev(str3), str3=%s\n",str3);
return 0;
}
Dr. K. Venkateshwalru 19/19
Strings in C
Out put:
str1=Anurag
str2=Anurag
str3=University
Strcmp(str2, str1)=0
str3=University
#include<stdio.h>
#include<string.h>
int main()
{
char name[10];
int i, length=0;
printf("\n Enter your name: ");
gets(name);
printf("\n name =");
puts(name);
// length of string with strlen
length=strlen(name);
printf("\n length of string=%d",length);
// length of string without strlen
Dr. K. Venkateshwalru 19/19
without Built-in functions
length=0;
for(i=0;name[i]!=’\0’;i++)
length++;
printf("\n length of string=%d",length);
}
Out put:
name =Anurag
length of string=6
length of string=6
Dr. K. Venkateshwalru 19/19
without Built-in functions
#include <stdio.h>
#include<string.h>
int main()
{ int c=0;
char s1[20],s2[20];
printf("\n Enter a string: ");
gets(s1);
for(int i=0;s1[i]!=’\0’;i++)
c++;
printf("\n Length of string s1=%d\n",c);
int j=0;
printf("\n string s1=%s\n",s1);
for(int i=c-1; s1[i]!=’\0’; i--)
s2[j++]=s1[i];
Dr. K. Venkateshwalru 19/19
without Built-in functions
Out put:
string s1=Hello hi
#include<stdio.h>
int main()
{
char s1[100], s2[100];
printf("\n Enter a string to check palindrome or not ");
gets(s1);
printf("\n String1= ");
puts(s1);
int length=0,i,c;
for(i=0; s1[i]!=’\0’;i++)
length++;
c=length;
printf("\n length of string1=%d", length);
for(i=0; s1[i]!=’\0’; i++)
Dr. K. Venkateshwalru 19/19
without Built-in functions
s2[i]=s1[--length];
printf("\n reverse of string1=");
puts(s2);
int flag=0;
for(i=0; i<c;i++)
{
if(s1[i]!=s2[i])
{
flag=1;
break;
}
}
if(flag==0)
printf("\n Given string %s is palindrome", s1);
else
printf("\n Given word %s is not a palindrome",s1);
}
Dr. K. Venkateshwalru 19/19
without Built-in functions
Out put:1
length of string1=5
reverse of string1=madam
Out put 2:
String1= ABCDE
length of string1=5
reverse of string1=EDCBA
s1[i]=s2[j++];
printf("\n After concatenation s1=%s",s1);
}
Out put:
Enter a string1:Hello
Enter a string2:Hi
length of s1=5
string1=Hello
string2=Hi