C Lab Programs

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

1

C Lab Programs’
1. Program to Check whether given number is Amstrong OR Not.

#include<stdio.h>
int main()
{
int a,s,i,j;
printf("Enter a:");
scanf("%d",&a);
s=0;
i=a;
while(a>0)
{
j=a%10;
s=s+(j*j*j);
a=a/10;
}
if(i==s)
{
printf("Given number is Amstrong");
}
else
{
printf("Given number is Not a Amstrong ");
}
return 0;
}

Sample Output:

Enter number:153
Given number is Amstrong.

2. Program to Check whether the given number is perfect OR Not.

#include<stdio.h>
int main()
{
int a,s,i,j;
printf("Enter number:");
scanf("%d",&a);
s=0;
i=a;
for(j=1;j<a;j++)
{ if(a%j==0)
2

{ s=s+j;
}
}
if(i==s)
{ printf("Given number is Perfect");
}
else
{ printf("Given number is Not a perfect");
}
return 0;
}

Sample Output:

Enter number:6
Given number is perfect.

3.Program Count the number of digits in a givne number.

#include<stdio.h>
int main()
{
int a,i,j;
printf("Enter number:");
scanf("%d",&a);
i=0;
while(a>0)
{ j=j%10;
i=i+1;
a=a/10;
}
printf("Number of digits in the given number is: ",i);
return 0;
}

Sample Output:

Enter number:123456789
Number of digits in the given number is: 9

4.Program to print the number of days in a month in a perticular year.

#include<stdio.h>
int main()
{
int month,year;
printf("Enter Year:");
scanf("%d",&year);
3

printf("1.January\n2.February\n3.March\n4.April\n5.May\n6.June\n7.July\n8.August
\n9.September\n10.October\n11.November\n12.December\n");
printf("Enter Month Number:");
scanf("%d",&month);
if(year%4==0)
{ if(month==2)
{ printf("The February month has 29 days in Leap Year\n");
}
}
else if(month==2)
{
printf("The February month has 28 days in normal Year\n");
}
else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 ||
month==12)
{
printf("Number of day in this month is 31\n");
}
else if(month==4 || month==6 || month==9 || month==11)
{
printf("Number of days in this month is 30\n");
}
else
{
printf("Month range is from 1 to 12\n");
}
return 0;
}

Sample Output:

Enter year:2000
1.January
2.February
3.March
4.April
5.May
6.June
7.July
8.August
9.September
10.October
11.November
12.December
Enter month number: 2
The February month has 29 days in Leap Year.
4

5.Program to Check whether the given number is a Strong OR Not.

#include<stdio.h>
int main()
{
int n,sum,i,f,r,temp;
printf("Enter number:");
scanf("%d",&n);
sum=0;
temp=n;
while(n>0)
{ i=1;
f=1;
r=n%10;
while(i<=r)
{ f=f*i;
i++;
}
sum=sum+f;
n=n/10;
}
if(temp==sum)
{
printf("Given number is strong\n");
}
else
{
printf("Given number is Not a strong\n");
}
return 0;
}

Sample Output:

Enter number:145
Given number is strong.

6.Program to print the LCM of two numbers.

#include<stdio.h>
int main()
{
int a,b,c,i,d;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
5

if(a>b)
{
d=a;
}

else
{ d=b;
}
for(i=d;i<=a*b;i++)
{ if(i%a==0 && i%b==0)
{
break;
}
}
printf("lcm is %d",i);
return 0;
}

Sample Output:

Enter two numbers: 12


23
lcm is 276.

7.Program to print HCF of two numbers.

#include<stdio.h>
int main()
{
int a,b,c,i,d;
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
c=1;
if(a>b)
{ d=a;
}
else
{ d=b;
}
for(i=1;i<=a && i<=b;i++)
{ if(a%i==0 && b%i==0)
{
d=c*i;
}
}
printf("hcf is %d",d);
return 0;
}
6

Sample Output:

Enter two numbers: 12


3
hcf is 3.

Arrays’
8. Program to Insert an element in an given array.

#include<stdio.h>
int main()
{
int i,b,c,e,f,n,x;
printf("Enter length of array:");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{
printf("Enter number:");
scanf("%d",&a[i]);
}
for(f=0;f<n;f++)
{
printf("%d ",a[f]);
}
printf("\n");
printf("Enter number to be added:");
scanf("%d",&b);
printf("Enter the position:");
scanf("%d",&c);
for(x=n-1;x>=c;x--)
{
a[x+1]=a[x];
a[x]=b;
}
for(e=0;e<=n;e++)
{
printf("%d ",a[e]);
}
return 0;
}

Sample Output:

Enter length of the array: 4


Enter element: 1
7

Enter element: 2
Enter element: 3
Enter element: 4
1 2 3 4
Enter number to be added: 100
Enter the position: 2
1 2 100 3

9.Program to delete an element form the given array.

#include<stdio.h>
int main()
{
int a,c,e,f;
printf("Enter length of array:");
scanf("%d",&a);
int b[a];
for(c=0;c<a;c++)
{ printf("Enter element:");
scanf("%d",&b[c]);
}
for(c=0;c<a;c++)
{ printf("%d\t",b[c]);
}
printf("\n");
printf("Enter position to be deleted:");
scanf("%d",&e);
for(f=e;f<a;f++)
{ b[f]=b[f+1];
}
for(c=0;c<a-1;c++)
{ printf("%d\t",b[c]);
}
printf("\n");
return 0;
}

Sample output:

Enter length of array: 4


Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
1 2 3 4
Enter position to be deleted: 2
1 2 4
8

10.Program to find the negetive elements in an array and count of that elements.

#include<stdio.h>
int main()
{
int n,i,c;
printf("Enter length of array:");
scanf("%d",&n);
int a[n];
for(i=0;i<n;i++)
{ printf("Enter number:");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{ printf("%d ",a[i]);
}
printf("\n");
c=0;
for(i=0;i<n;i++)
{ if(a[i]<0)
{ printf("The -ve number at position a[%d] is %d\n",i,a[i]);
c=c+1;
}
}
printf("number of -ve digits is %d\n",c);
return 0;
}

Sample output:

Enter length of array: 4


Enter element: 23
Enter element: -32
Enter element: -1
Enter element: 4
23 -32 -1 4
The -ve number at position a[1] is -32
The -ve number at position a[2] is -1

11.Program to shift an array to left once.

#include<stdio.h>
void main()
{
int b,c,n;
printf("Enter length of an array:");
9

scanf("%d",&n);
int a[n];
for(b=0;b<n;b++)
{ printf("Enter element:");
scanf("%d",&a[b]);
}
for(b=0;b<n;b++)
{ printf("%d ",a[b]);
}
printf("\n");
c=a[0];
for(b=0;b<n;b++)
{ a[b]=a[b+1];
}
a[n-1]=c;
printf("After left shift:");
for(b=0;b<n;b++)
{ printf("%d",a[b]);
}
return 0;
}

Sample output:

Enter length of array: 3


Enter element: 1
Enter element: 2
Enter element: 3
1 2 3
After left shift: 2 3 1

12.Program to shift an array to right.

#include<stdio.h>
int main()
{
int b,c,n;
printf("Enter length of an array:");
scanf("%d",&n);
int a[n];
for(b=0;b<n;b++)
{ printf("Enter element:");
scanf("%d",&a[b]);
}
for(b=0;b<n;b++)
{ printf("%d ",a[b]);
}
printf("\n");
c=a[n-1];
10

for(b=n-2;b>-1;b--)
{ a[b+1]=a[b];
}
a[0]=c;
printf("After right shift:")
for(b=0;b<n;b++)
{ printf("%d ",a[b]);
}
return 0;
}

Sample output:

Enter length of array: 3


Enter element: 1
Enter element: 2
Enter element: 3
1 2 3
After right shift: 3 1 2

13.Program to Reverse an Array of Elements.

#include <stdio.h>
int main()
{
int h,k,m,b;
printf("Enter No.of Elements : ");
scanf("%d",&k);
int a[k];
for(h=0;h<k;h++)
{
printf("Enter The Element No.%d : ",h+1);
scanf("%d",&a[h]);
}
printf("Array Before Reversing : ");
for (b=0;b<=k-1;b++)
{
printf("%d",a[b]);
}
printf("\n");
printf("Array After Reversing : ");
for (b=0;b<k;b++)
{
printf("%d",a[k-1-b]);
}
printf("\n");
}
11

SAMPLE OUTPUT:

Enter No.of Elements : 3


Enter The Element No.1 : 1
Enter The Element No.2 : 4
Enter The Element No.3 : 6
Array Before Reversing : 1 4 6
Array After Reversing : 6 4 1

14.Program for Linear Searching:

#include<stdio.h>
int main()
{
int h,k,i,j,m,z;
printf("Enter No.of Elements : ");
scanf("%d",&k);
int arr[k];
for(h=0;h<k;h++)
{
printf("Enter The Element No.%d : ",h+1);
scanf("%d",&arr[h]);
}
printf("Enter the Element To Search: ");
scanf("%d",&i);
for(j=0;j<=k-1;j++)
{
if (arr[j]==i)
{ z=1;
m=j;
break;
}
}
if (z==1)
{
printf("The Number %d Exist in an Array i.e; arr[%d]=%d.\n",i,m,i);
}
else
{
printf("The Number %d is Not Found in an Array.\n",i);
}
return 0;
}
12

SAMPLE OUTPUT:

CASE :1

Enter No.of Elements : 3


Enter The Element No.1 : 1
Enter The Element No.2 : 5
Enter The Element No.3 : 3
Enter the Element To Search: 5
The Number 5 Exist in an Array i.e; arr[1]=5.

CASE :2

Enter No.of Elements : 3


Enter The Element No.1 : 1
Enter The Element No.2 : 2
Enter The Element No.3 : 3
Enter the Element To Search: 4
The Number 4 is Not Found in an Array.

15.Program for Binary Searching:

#include <stdio.h>
int main() {
int E,x,yes,cen=0,q,k,w;
printf("Enter No.of Elements : ");
scanf("%d",&x);
int a[x];
printf("Enter Sorted Elements : ");
printf("\n");
for(k=0;k<x;k++)
{
printf("Enter The Element No.%d : ",k+1);
scanf("%d",&a[k]);
}
printf("The List is : ");
for(k=0;k<x;k++)
{
printf("%d,",a[k]);
}
printf("\n");
printf("Enter Element To Search : ");
scanf("%d",&E);
w=0;
q=x-1;
yes=0;
while (w<=q)
{ cen=(w+q)/2;
if (E<a[cen])
13

q=cen-1;
else if(E>a[cen])
w=cen+1;
else if(E==a[cen])
{printf("The Element is : %d & Present At the Index Position:- %d.\n",E,cen);
yes=1;
break;
}
}
if(yes==0)
{ printf("Element %d Not Found.\n",E);
}
return 0;
}

SAMPLE OUTPUT:

CASE:1

Enter No.of Elements : 5


Enter Sorted Elements :
Enter The Element No.1 : 1
Enter The Element No.2 : 4
Enter The Element No.3 : 7
Enter The Element No.4 : 8
Enter The Element No.5 : 9
The List is : 1,4,7,8,9,
Enter Element To Search : 4
The Element is : 4 & Present At the Index Position:- 1.

16.Program To Count No.of Elements Present in an Array:-

#include<stdio.h>
int main()
{
int s,t,v,w,z,u,no;
printf("Enter the No.of Elements in an Array :");
scanf("%d",&s);
int a[s];
for (w=0;w<s;w++)
{
printf("Enter the Element No.%d : ",w+1);
scanf("%d",&a[w]);
}
printf("The Array is : ");
for (w=0;w<s;w++)
{
printf("%d,",a[w]);
}
14

printf("\n");
for(v=0;v<s;v++)
{
u=0;
for(w=0;w<s;w++)
{
if(a[v]==a[w])
{
u++;
}
}
printf("The count of Number %d is : %d.\n",a[v],u);}
}
return 0;
}

SAMPLE OUTPUT:

Enter the No.of Elements in an Array :4


Enter the Element No.1 : 1
Enter the Element No.2 : 2
Enter the Element No.3 : 3
Enter the Element No.4 : 4
The Array is : 1,2,3,4
The count of Number 1 is : 1.
The count of Number 2 is : 1.
The count of Number 3 is : 1.
The count of Number 4 is : 1.

2-D Arrays
17.Program to Sum of The Two Matrices :

#include<stdio.h>
int main()
{
int n,m,c,d,s,t,u,v,x;
printf("Enter number of rows:");
scanf("%d",&n);
printf("Enter number of columns:");
scanf("%d",&m);
int a[n][m];
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("Enter element:");
scanf("%d",&a[c][d]);
}
}
15

printf("First matrix is:");


for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("%d ",a[c][d]);
}
printf("\n");
}
printf("\n");
printf("Enter number of rows for second matrix:");
scanf("%d",&s);
printf("Enter number of columns for second matrix:");
scanf("%d",&t);
int z[s][t];
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("Enter element:");
scanf("%d",&z[u][v]);
}
}
printf("Second matrix is:");
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("%d ",z[u][v]);
}
printf("\n");
}
printf("\n");
if(n==s && m==t)
{ printf("Sum of matrix is ");
for(u=0;u<n;u++)
{ for(v=0;v<m;v++)
{ x=a[u][v]+z[u][v];
printf("%d ",x);
}
printf("\n");
}
printf("\n");
}
else
{ printf("Sum should done only if size of two matrices are same");
}
}
SAMPLE OUTPUT:

Enter number of rows: 2


Enter number of columns: 2
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
16

First matrix is:


1 2
3 4

Enter number of rows for second matrix: 2


Enter number of columns for second matrix: 2
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
Second matrix is:
1 2
3 4
Sum of matrix is :
2 4
6 8

18.Program to Subtraction of Two Matrices:

#include<stdio.h>
int main()
{
int n,m,c,d,s,t,u,v,x;
printf("Enter number of rows:");
scanf("%d",&n);
printf("Enter umber of columns:");
scanf("%d",&m);
int a[n][m];
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("Enter element:");
scanf("%d",&a[c][d]);
}
}
printf("First matrix is:");
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("%d ",a[c][d]);
}
printf("\n");
}
printf("\n");
printf("Enter number of rows for second matrix:");
scanf("%d",&s);
printf("Enter number of columns for second matrix:");
scanf("%d",&t);
int z[s][t];
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("Enter element:");
17

scanf("%d",&z[u][v]);
}
}
printf("Second matrix is:");
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("%d ",z[u][v]);
}
printf("\n");
}
printf("\n");

if(n==s && m==t)


{ printf("difference of matrix is ");
for(u=0;u<n;u++)
{ for(v=0;v<m;v++)
{ x=a[u][v]-z[u][v];
printf("%d ",x);
}
printf("\n");
}
printf("\n");
}
else
{ printf("Difference should done only if size of two matrices are same");
}
}

SAMPLE OUTPUT:

Enter number of rows: 2


Enter number of columns: 2
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
First matrix is:
1 2
3 4

Enter number of rows for second matrix: 2


Enter number of columns for second matrix: 2
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
Second matrix is:
1 2
3 4
18

difference of matrix is :
0 0
0 0

19. Program to Multiply Two Matrices :

#include<stdio.h>
int main()
{
int n,m,c,d,s,t,u,v;
printf("Enter number of rows:");
scanf("%d",&n);
printf("Enter umber of columns:");
scanf("%d",&m);
int a[n][m];
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("Enter element:");
scanf("%d",&a[c][d]);
}
}
printf("First matrix is:");
for(c=0;c<n;c++)
{ for(d=0;d<m;d++)
{ printf("%d ",a[c][d]);
}
printf("\n");
}
printf("\n");
printf("Enter number of rows for second matrix:");
scanf("%d",&s);
printf("Enter number of columns for second matrix:");
scanf("%d",&t);
int z[s][t];
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("Enter element:");
scanf("%d",&z[u][v]);
}
}
printf("Second matrix is:");
for(u=0;u<s;u++)
{ for(v=0;v<t;v++)
{ printf("%d ",z[u][v]);
}
printf("\n");
}
printf("\n");
int x[10][10];
19

for(c=0;c<n;c++)
{ for(d=0;d<t;d++)
{ x[c][d]=0;
}
}
printf("After multiplication:");
for(c=0;c<n;c++)
{ for(d=0;d<t;d++)
{ for(u=0;u<s;u++)
{ x[c][d]=x[c][d]+a[c][u]*z[u][d];
}
}
}
for(c=0;c<n;c++)
{ for(d=0;d<t;d++)
{ printf("%d ",x[c][d]);

}
printf("\n");
}
printf("\n");
}

Sample output:

Enter number of rows: 2


Enter number of columns: 2
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
First matrix is:
1 2
3 4

Enter number of rows for second matrix: 2


Enter number of columns for second matrix: 2
Enter element: 1
Enter element: 2
Enter element: 3
Enter element: 4
Second matrix is:
1 2
34

After multiplication:
7 10
15 22
20

20. Program to check whether the given matrix is symmetric OR Not

#include<stdio.h>
int main()
{
int a,b,d,e;
printf("Enter length of rows:");
scanf("%d",&b);
printf("Enter number of column:");
scanf("%d",&a);
int c[b][a];
int x[10][10];
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ printf("Enter element:");
scanf("%d",&c[d][e]);
}
}
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ x[e][d]=c[d][e];
}
}
printf("\n");
printf("After transposing the matrix:");
printf("\n");
for(d=0;d<a;d++)
{ for(e=0;e<b;e++)
{ printf("%d ",x[d][e]);
}
printf("\n");
}
if(a==b)
{ for(d=0;d<a;d++)
{ for(e=0;e<b;e++)
{ if(c[d][e]==x[d][e])
{
}
else
{ goto new;
}
}
}
printf("Matric is Symmetric");
}
else
{ new:printf("Matrix is not symmetric");
}
}
21

Sample output:

Enter length of rows: 2


Enter number of column: 2
Enter element: 1
Enter element:2
Enter element: 3
Enter element : 4

After transposing the matrix:


1 3
2 4
Matrix is not symmetric

21. Program to find transpose of a given matrix.

#include<stdio.h>
int main()
{
int a,b,d,e;
printf("Enter length of rows:");
scanf("%d",&b);
printf("Enter lenght of column:");
scanf("%d",&a);
int c[b][a];
int x[10][10];
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ printf("Enter element:");
scanf("%d",&c[d][e]);
}
}
printf("Given matrix is:\n");
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ printf("%d ",c[d][e]);
}
printf("\n");
}
for(d=0;d<b;d++)
{ for(e=0;e<a;e++)
{ x[e][d]=c[d][e];
}
}
printf("\n");
printf("After transposing the matrix:");
22

printf("\n");
for(d=0;d<a;d++)
{ for(e=0;e<b;e++)
{ printf("%d ",x[d][e]);
}
printf("\n");
}
}

Sample Output:

Enter length of rows: 2


Enter lenght of column: 2
Enter element: 1
Enter element : 2
Enter element: 3
Enter element: 4
Given matrix is:
1 2
3 4
After transposing the matrix:
1 3
2 4

22.Program to Bubble Sort a List of Elements:-

#include <stdio.h>
int main()
{
int y,s,r,t,Hi,w;
printf("Enter No.of Elements : ");
scanf("%d",&t);
int a[t];
printf("Enter Those %d Elements of an Array: \n",t);
for(s=0;s<t;s++)
{
printf("Enter The Element No.%d : ",s+1);
scanf("%d",&a[s]);
}
printf("The Array Before Sorting : \n");
for(r=0;r<t;r++)
{
if (r==t-1)
{ .printf("%d .",a[r]);
}
else
{ printf("%d,",a[r]);
}
}
23

for (r=0;r<t-1;r++)
{
for (s=0;s<t-1-r;s++)
{
if (a[s]>a[s+1])
{
Hi=a[s];
a[s]=a[s+1];
a[s+1]=Hi;
}
}
}
printf("\n");
printf("The Array After Sorting : \n");
for(r=0;r<t;r++)
{
if (r==t-1)
{
.printf("%d.",a[r]);
}
else
{
printf("%d,",a[r]);
}
}
printf("\n");
return 0;
}

Sample output:

Enter No.of Elements : 5


Enter Those 5 Elements of an Array:
Enter The Element No.1 : 7
Enter The Element No.2 : 2
Enter The Element No.3 : -9
Enter The Element No.4 : 0
Enter The Element No.5 : -3
.The Array Before Sorting : 7,2,-9,0,-3.
.The Array After Sorting : -9,-3,0,2,7.

23.Program to perform Selection Sorting of Elements in an Array:

#include<stdio.h>
int main()
{
int w,g,h,x,v,min,Bye;
printf("Enter No.of Elements : ");
scanf("%d",&x);
int hi[x];
24

for(v=0;v<x;v++)
{
printf("Enter Element NO.%d : ",v+1);
scanf("%d",&hi[v]);
}
printf("\n");
printf("The Array Before Sorting is : ");
for(v=0;v<x;v++)
{
printf("%d,",hi[v]);
}
for (g=0;g<x-1;g++)
{
min=g;
for(w=g+1;w<x;w++)
{ if (hi[w]<hi[min])
{ min=w;
}
}
Bye=hi[g];
hi[g]=hi[min];
hi[min]=Bye;
}
printf("\n");
printf("The Array After Sorting is : ");
for(v=0;v<x;v++)
{
printf("%d,",hi[v]);
}
printf("\n");
}

SAMPLE OUTPUT:-

Enter No.of Elements : 5


Enter Element NO.1 : 9
Enter Element NO.2 : 0
Enter Element NO.3 : -5
Enter Element NO.4 : 98
Enter Element NO.5 : -1

The Array Before Sorting is : 9,0,-5,98,-1,


The Array After Sorting is : -5,-1,0,9,98,
25

24.Program To Perform Insersion Sorting of An Elements in An Array:-

#include<stdio.h>
int main()
{
int u,v,w,x,Hi;
printf("Enter No.of Elements in an Array : ");
scanf("%d",&x);
int a[x*x];
for(w=0;w<x;w++)
{
printf("Enter The Element No.%d : ",w+1);
scanf("%d",&a[w]);
}
printf("\n");
printf("The Array Before Sorting : ");
for(w=0;w<x;w++)
{
printf("%d,",a[w]);
}
printf("\n");
for(u=1;u<=x-1;u++)//u<x
{
v=u;
while(v>0 && a[v-1] > a[v])
{
Hi = a[v];
a[v] = a[v-1];
a[v-1] = Hi;
v--;
}
}
printf("Sorted List in Ascending Order : ");
for(w=0;w<x;w++)
{
printf("%d,",a[w]);
}
printf("\n");
}

SAMPLE OUTPUT:-

Enter No.of Elements in an Array : 5


Enter The Element No.1 : 0
Enter The Element No.2 : 1
Enter The Element No.3 : -98
Enter The Element No.4 : -3
Enter The Element No.5 : 4
The Array Before Sorting : 0,1,-98,-3,4,
Sorted List in Ascending Order : -98,-3,0,1,4,
26

Character Arrays’
25.Program To Reverse an String And Whether it is Palindrome Or Not:-

#include<stdio.h>
#include<string.h>
int main()
{
int l,e=0;
char t[1000],v[1000];
printf("Enter The String : ");
gets(t);
int c=strlen(t);
for (l=0;l<=c-1;l++)
{ v[l]=t[l];
}
printf("The Reverse of String is : ");
for (l=c-1;l>=0;l--)
{ printf("%c",v[l]);
}
for (l=0;l<=c-1;l++) //c-1 last index value of Element
{ if(t[l]==t[c-1-l])
{ e++;
}
}
if(e==c)
{ printf("\nThe Given String '%s' is Palindrome.\n",t);
}
else if(e!=c)
{ printf("\nThe Given String '%s' is NOT Palindrome.\n",t);
}
}

SAMPLE OUTPUT:-

Enter The String : Hi How R U


The Reverse of String is : U R woH iH
The Given String 'Hi How R U' is NOT Palindrome.

26.Program To Contenate Two Strings WithOut Function:-

#include<stdio.h>
#include<string.h>
int main()
{
char x[1000],y[1000],r[1000];
printf("Enter The String : ");
27

gets(x);
printf("Enter The String : ");
gets(y);
int z=strlen(x),k=strlen(y);
int i=0,j;
for(;i<=z-1;i++)
{ r[i]=x[i];
}
for(j=z;j<=z+k-1;j++)
{ r[j]=y[j-z];
}
printf("Concatenation of Strings is : %s.\n",r);
}

SAMPLE OUTPUT:-

Enter The String : Hi Who Are You


Enter The String : .I'm Fine.
Concatenation of Strings is : Hi Who Are You.I'm Fine..

27.Program to Compare Two Strings:-

#include<stdio.h>
#include<string.h>
int main()
{
char o[1000],p[1000];
printf("Enter The 1st String : ");
gets(o);
printf("Enter The 2nd String : ");
gets(p);
int z=strlen(o),y=strlen(p),s=0,u=0,v=0;
for(int h=0;o[h]!='\0';h++)
{ u++;
}
for(int f=0;p[f]!='\0';f++)
{ v++;
}
if(u==v)
{
for(int k=0;k<=u-1;k++)
{
if(o[k]==p[k])
{ s++;
}
}
if(s==u)
{ printf("%d\nStrings are Equal.\n",o[0]-p[0]);
}
28

else
{ printf("%d\nStrings Are Not Equal.\n",o[0]-p[0]);
}
}
else
{ printf("%d\nStrings Are Not Equal.\n",o[0]-p[0]);
}
}

SAMPLE OUTPUT:-

CASE:1
Enter The 1st String : Excuse Me.
Enter The 2nd String : Where The IIT clg is?
-18
Strings Are Not Equal.

CASE:2
Enter The 1st String : Hi
Enter The 2nd String : Hi
0
Strings are Equal.

28.Program To Perform Conctenation Of Two Strings Using Function:-

#include<stdio.h>
#include<string.h>
int main()
{
char Hai[1000],Bye[1000];
printf("Enter the 1st String : ");
gets(Hai);
printf("Enter the 2nd String : ");
gets(Bye);
printf("The 1st String is : ");
puts(Hai);
printf("The 2nd String is : ");
puts(Bye);
strcat(Hai,Bye);
puts(Hai);
}

SAMPLE OUTPUT:

Enter the 1st String : Hi


Enter the 2nd String : Hello How r u
The 1st String is : Hi
The 2nd String is : Hello How r u
Hi Hello How r u
29

29.Program To Perform Comparing Of two Strings Using Functions:-

#include<stdio.h>
#include<string.h>
int main()
{
char r[1000],t[1000];
printf("Enter the 1st String : ");
gets(r);
printf("Enter the 2nd String : ");
gets(t);
int Hi=strcmp(r,t);
if (Hi==0)
{ printf("Strings Are Equal.\n");
}
else
{ printf("Strings Are Not Equal.\n");
}
printf("Strcmp() Value is : %d.\n",Hi);
}

SAMPLE OUTPUT:-

CASE:1

Enter the 1st String : GOOD MRNG


Enter the 2nd String : MAM
Strings Are Not Equal.
Strcmp() Value is : -6.

CASE:2

Enter the 1st String : Name


Enter the 2nd String : Name
Strings Are Equal.
Strcmp() Value is : 0.

30.Program to Perform Copy of a String Into Another String:-

#include<stdio.h>
#include<string.h>
int main()
{
char Tom[1000],Cat[1000];
printf("Enter the 1st String : ");
gets(Tom);
printf("Enter the 2nd String : ");
gets(Cat);
strcpy(Cat,Tom);
30

puts(Tom);
printf("The Copy of String is : ");
puts(Cat);
}

SAMPLE OUTPUT:-

Enter the 1st String : Bye Bro!


The Copy of String is : Bye Bro!

31.Program To Calculate String Length WithOut Using Functions:-

#include<stdio.h>
int main()
{
int x=0,b=0;
char j[1000];
printf("Enter the String : ");
gets(j);
for(b=0;j[b]!='\0';)
{
b++;
}
printf("The Length of String is : %d.\n",b);
}

SAMPLE OUTPUT:-

Enter the String : Im Naughty Boy


The Length of String is : 14.

32.Program To Find the Length of Two Strings With Using Function:-

#include<stdio.h>
#include<string.h>
int main()
{
char p[1000],q[1000];
printf("Enter the 1st String : ");
gets(p);
printf("Enter the 2nd String : ");
gets(q);
int k=strlen(p),z=strlen(q);
printf("\nThe Length of the String %s is : %d.\nThe Length of the String %s is : %d.\n",p,k,q,z);
}
31

SAMPLE OUTPUT:-

Enter the 1st String : No Way


Enter the 2nd String : I'll Not
The Length of the String No Way is : 6.
The Length of the String I'll Not is : 8.

33.Program To Change Lower Case Charcters Into Upper Case Characters And
Upper to Lower:-

#include<stdio.h>
int main()
{
int k;
char e[1000];
printf("Enter the String : ");
gets(e);
for (k=0;e[k]!='\0';k++)
{
if(e[k]>='a' && e[k]<='z')
{ e[k]=e[k]-32;
}
else if(e[k]>='A' && e[k]<='Z')
{ e[k]=e[k]+32;
}
}
printf("The New String is : %s\n",e);
}

SAMPLE OUTPUT:-

Enter the String : Im the Only Waste Fellow Who is Interested in


The New String is : iM THE oNLY wASTE fELLOW wHO IS iNTERESTED IN

34.Program To Count No.of Words Present in the Sentence or in a Paragraph.

#include<stdio.h>
#include<string.h>
int main()
{
int a=0,b,count=0;
char s[100];
printf("Enter string:");
gets(s);
while(s[a]!='\0')
{ a++;
}
printf("length of string is : %d\n",a);
for(b=0;b<=a;b++)
32

{ if(s[b]==' ')
{ count++;
}
else if(s[b]=='\0')
{ count++;
}
}
printf("count of words in string is : %d\n",count);
}

Sample Output:

Enter string: We are students of S03


length of string is : 22
count of words in string is : 5

You might also like