UNIT-2 C Programming
UNIT-2 C Programming
1. Introduction to Arrays
An Array is a collection of similar data elements. These data elements have same data
types
Array is a collection of elements of same data type stored under common name.
An array is a data structure that is used for storage of homogeneous data.
It is classified into three types
i) One dimensional Array
ii) Two dimensional Array
iii) Multi-dimensional Array
1.1 Characteristics of an Array
The elements of the array are stored in continuous memory location.
Individual elements of an array can be accessed with the integer value known as index.
Array index starts from 0.
Sample program1:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[4]={20,45,67,89};
printf(“%d \t%d\t%d\t%d”,a[0],a[1],a[2],a[3]);
getch();
}
Output:
20 45 67 89
Sample program2:
The efficient way of accessing array elements is using loop
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[4]={20,45,67,89};
int i;
for(i=0;i<4;i++)
{
printf(“%d\t”,a[i]);
}
getch();
}
Output:
20 45 67 89
Note:
By default the elements of an array are not initialized. They may contain some garbage
value, so before using array we must initialize the array or read some meaningful data into it.
2.3 Run time initialization:
int a[4];
for(i=0;i<4;i++)
{
scanf(“%d”,&a[i]);
}
Sample program 3
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[4],i;
Example:
int a[3][2]={{10,20},{30,40},{50,60}};
Col 0 Col 1
Row 0 10 20
Row 1 30 40
Row 2 50 60
Sample program 4:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][2]={{10,20},{30,40},{50,60}};
printf(“%d\t”,a[0][0]);
printf(“%d\n”,a[0][1]);
printf(“%d\t”,a[1][0]);
printf(“%d\n”,a[1][1]);
printf(“%d\t”,a[2][0]);
printf(“%d\n”,a[2][1]);
}
Output:
10 20
30 40
50 60
Sample program 5
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2]={{10,20},{30,40},{50,60}};
int i,j;
printf("\nThe Matrix");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",a[i][j]);
}
}
getch();
}
Output:
10 20
30 40
50 60
4.2 Run time initialization:
Two-dimensional array can be initialization at run time using loop.
int a[2][3];
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”, &a[i][j]);
}
}
Sample program 5:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][3];
int i,j;
printf(“Enter the elements\n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
{
scanf(“%d”, &a[i][j]);
}
}
Output:
Enter the elements
1
2
3
4
The elements are
1 2
2 4
4. String operations
Array of characters:
I Y E A R \0
3.To get a string input using scanf and display using printf:
#include<stio.h>
#include<conio.h>
void main()
{
char name[10];
printf(“Enter the string : ”);
scanf(“%s”, name);
printf(“The string is: %s”,name);
getch();
}
Output:
Enter the string: IYEAR
The string is: IYEAR
4.To get a string input using gets and display using puts:
#include<stdio.h>
#include<conio.h>
void main()
{
char name[10];
printf(“Enter the string : ”);
gets(name);
puts(name);
getch();
}
Output:
Enter the string: IYEAR
The string is: IYEAR
5. String functions
In C there are large number of string function. These functions use the standard header file
string.h.
5.1 C string functions are:
1.strlen(s1)
2.strcpy(s1,s2)
3.strcmp(s1,s2)
4.strcat(s1,s2)
5.strcmpi(s1,s2)
6.strrev(s1)
7.strlwr(s1)
8.strupr(s1)
9.strstr(s1,s2)
1.strlen(s1)
It is returns the length of a string.
Syntax:
strlen (“string”);
or
variable = strlen(“string”);
or
strlen (variablename);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
clrscr();
printf("Enter the string");
gets(s1);
printf("The length of the string",strlen(s1));
getch();
}
Output:
Enter the string
C Program
The length of the string is 9
2.strcpy(s1,s2)
It copy the string s2 to the string s1.
Syntax:
strcpy (s1 , s2);
#include <stdio.h>
#include <string.h>
void main()
{
char s1[10], s2[10];
printf("Input a string\n");
gets(s1);
strcpy(s2, s1);
printf("Source string:%s\n", s1);
printf("Destination string: %s\n", s2);
getch();
}
Output :
Input a string: 1 year
Source string: 1 year
Destination string: 1 year
3.strcmp(s1,s2)
It compares two strings. string s1 compared with string s2 character by character
until the corresponding character differs or until the end of the string is reached.
Returns 0 if s1 is same as s2.
Returns <0 if sl < s2.
Returns >0 if s1 > s2
Syntax:
strcmp(s1,s2);
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
int res;
clrscr();
printf("Enter String1\n");
gets(s1);
printf("Enter String2\n");
gets(s2);
res=strcmp(s1,s2);
if(res==0)
{
printf("\nString are equal");
}
else
{
printf("\nStrings are not equal");
}
getch();
}
Output:
Enter String1
Hello
Enter String2
Hai
Strings are not equal
4.strcat(s1,s2 )
String concatenate function append(adding) a string s2 to the string s1 and
returns a pointer to string s1.
Syntax:
strcat(s1,s2);
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
int res;
clrscr();
printf("Enter String1\n");
gets(s1);
printf("Enter String2\n");
gets(s2);
res=strcmpi(s1,s2);
if(res==0)
{
printf("\nString are equal");
}
else
{
printf("\nStrings are not equal");
}
getch();
}
Output:
Enter String1
Hello
Enter String2
hello
Strings are equal
6.strrev()
It reverses the given string.
Syntax:
strrev(s1);
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10];
clrscr();
printf("Enter the string");
gets(s1);
printf("Reverse of the string",strrev(s1));
getch();
}
Output:
Enter the string: civil
Reverse of the string: livic
7.strlwr()
Converts given string to lowercase.
Syntax:
strlwr(s1);
8.strupr()
Converts given string to uppercase.
Syntax:
strupr(s2);
datatype arrayname[row-size][column-size];
Example:
char a[4][7];
//it can store 4 strings and each string stores maximum 7 characters
Initialization of String of Array:
char A[4][5]={“Ram”,”Raj”,”Ravi”,”Jai”};
A[0] R a m \0
A[1] R a j \0
A[2] R a v i \0
A[3] J a i \0
1.Example Program For Sorting Of String In C Language
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
int i, j, n;
char str[20][20],temp[20];
clrscr();
printf("Enter the no. of strings to be sorted");
scanf("%d",&n);
printf("\nEnter the string");
for(i=0;i<n;i++)
{
scanf("%s",str[i]);
}
for(i=0;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf("\nThe sorted strings\n");
for(i=0;i<n;i++)
{
printf("\n%s",str[i]);
}
getch();
}
Output:
}
output
Enter the string:c program
Reverse string is:margop c
iii)converting upper case string to lower case string without using string function
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],lower[20];
int i=0;
clrscr();
printf("\nEnter the string in upper case:");
gets(s1);
while(s1[i])
{
if(s1[i]>='A' && s1[i]<='Z')
lower[i]=s1[i]+32;
else
lower[i]=s1[i];
i++;
lower[i]='\0';
printf("\nLower String");
puts(lower);
getch();
Output:
Enter the string in upper case: C PROGRAM
Lower String c program
iv) Converting lower case string to upper case string without using string function
#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],upper[20];
int i=0;
clrscr();
printf("\nEnter the string:");
gets(s1);
while(s1[i])
{
if(s1[i]>='a' && s1[i]<='z')
upper[i]=s1[i]-32;
else
upper[i]=s1[i];
i++;
}
upper[i]='\0';
printf("\nUpper String");
puts(upper);
getch();
}
output:
Enter the string in lower case: c program
Upper String C PROGRAM
v) Concatenating two string without using string function
#include<stdio.h>
#include<string.h>
void main()
{
char s1[50], s2[30];
int i,j=0;
clrscr();
printf("\nEnter String 1 :");
gets(s1);
printf("\nEnter String 2 :");
gets(s2);
i = strlen(s1);
while(s2[j]!='\0')
{
s1[i]=s2[j];
i++;
j++;
}
s1[i] = '\0';
printf("\nconcated string is:%s",s1);
getch();
}
output:
Enter String 1:computer
Enter String 2:programming
Concated string is : computerprogramming
6. Illustrative problems:
Enter Elements
2
3
4
5
6
The Sum of elements in an array is 20
The Avg of elements in an array is 4
Output:
Enter Matrix A
12
34
Enter Matrix B
12
34
The result
24
68
C Program to print Transpose of matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2] , i, j;
clrscr();
printf("Enter matrix A");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",a[i][j]);
}
}
printf("\nTranspose");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",a[j][i]);
}
}
getch();
}
Output:
Enter matrix A
1 2
3 4
Transpose
1 3
2 4
6.3 Palindrome of a given String.
Example 1
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(){
char str[20];
int i, len, temp=0;
int flag = 0;
printf("Enter a string:");
scanf("%s", str);
len = strlen(str);
for(i=0;i < len ;i++)
{
if(str[i] != str[len-i-1])
{
temp = 1;
break;
}
}
if (temp==0) {
printf("String is a palindrome");
}
else {
printf("String is not a palindrome");
}
return 0;
}
Output:
Enter a string: madam
String is a palindrome
#include <stdio.h>
#include <string.h>
void main()
{
char s1[1000],s2[1000];
printf("Enter the string: ");
scanf("%s", s1);
strcpy(s2,s1);
strrev(s2);
if(!strcmp(s1,s2))
{
printf("string is palindrome");
}
else
{
printf("string is not palindrome");
}
}
Output:
Enter a string: madam
String is a palindrome
Output:
Enter Matrix A
12
34
Enter Matrix B
11
11
The result
01
23c
C Program to Print Matrix Multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2]={0};
int i,j,k;
clrscr();
printf("Enter Matrix A");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("\nThe result");
for(i=0;i<2;i++)
{
printf("\n");
for(j=0;j<2;j++)
{
printf("\t%d",c[i][j]);
}
}
getch();
}
Output:
Enter Matrix A
12
34
Enter Matrix B
12
34
The result
7 10
15 22
#include <stdio.h>
#include<conio.h>
int main()
{
int A[3][3]={1,2,3,4,5,6,7,8,9};
int i,j,num;
clrscr();
#include <stdio.h>
int main()
{
int A[2][2];
int i,j;
long det;
printf("Enter elements in matrix of size 2x2: \n");
for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
scanf("%d", &A[i][j]);
}
}
det = (A[0][0] * A[1][1]) - (A[0][1] * A[1][0]);
printf("Determinant of matrix A = %ld", det);
return 0;
}
#include<stdio.h>
int main()
{
int i,j, a[3][3];
int x, y, z, Determinant = 0;
Output:
Output:
4.Sorting the given array elements in ascending order and printing smallest and largest
element in the array
#include<stdio.h>
#include<conio.h>
void main()
{
int s,i,j,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
{
scanf("%d",&a[i]);
}
for(i = 0; i < s; i++)
{
for( j=i+1 ;j < s ;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
{
printf(" %d",a[i]);
}
printf(“Largest element in the array : %d ”,a[s -1]);
printf(“Smallest element in the array :%d ”, a[0]);
getch();
}
Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 2 7
The array after sorting is: 0 2 4 5 7
Largest element in the array : 7
Smallest element in the array : 0
4.Sorting the given array elements in ascending order
#include<stdio.h>
#include<conio.h>
void main()
{
int s,i,j,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<s;i++)
{
for(j=i+1;j<s;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("After sorting is: ");
for(i=0;i<s;i++)
{
printf(" %d",a[i]);
}
getch();
}
Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is: 0 4 5 7 21