Concatanate 2 strings

Program to concatanate 2 entered strings ( without using inbuilt function )

#include<stdio.h>
#include<conio.h>
main()
{
char a[20],b[20];
int i,l1=0,l2=0,j=0;
clrscr();
printf("Enter the string A ");
gets(a);
printf("Enter the string B ");
gets(b);
for(i=0;a[i]!='\0';i++)
l1++;
for(i=0;b[i]!='\0';i++)
l2++;
for(i=l1;j<l2;i++)
{
a[i]=b[j];
j++;
}
a[i]='\0';
printf("Concatenated string is ");
puts(a);
getch();
return 0;
}

Palindrome check for a string

Program to check if the entered string is a palindrome or not.

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char a[20],rev[20];
int i,l=0,flag=0;
clrscr();
printf("Enter the string\n");
gets(a);
for(i=0;a[i]!='\0';i++)
l++;
for(i=0;i<l;i++)
{
rev[i]=a[l-1-i];
}
rev[i]='\0';
for(i=0;i<l;i++)
{
if(a[i]!=rev[i])
{
flag=1;
break;
}
}
printf("Entered string is %s \n",a);
printf("Reversed string is %s \n",rev);
if(flag)
printf("String is not a palindrome");
else
printf("String is a palindrome");
getch();
return 0;
}


Linear search

Program to search for a key element in an entered array using linear search.


#include<stdio.h>
#include<conio.h>
main()
{
int a[30],i,j,key,n,m,flag=0;
clrscr();
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element\n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
j=i;
if(flag)
{
printf("The key element is found in the position %d\n",i+1);
printf("Enter the no which is to be replaced with the key element\n");
scanf("%d",&m);
a[j]=m;
printf("Array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
else
printf("Key element is not found in the array");
getch();
return 0;
}

Palindrome check for an array

Program to check if the entered array is a palindrome or not..

#include<stdio.h>
#include<conio.h>
main()
{
int a[30],b[30],i,n,swap,temp=0;
clrscr();
printf("Enter n\n" );
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
b[n-i-1]=a[i];
}
for(i=0;i<=(n/2);i++)
{
swap=a[i];
a[i]=a[n-i-1];
a[n-i-1]=swap;
}
for(i=0;i<n;i++)
{
if(a[i]==b[i])
temp=1;
}
printf("Reversed array is \n");
for(i=0;i<n;i++)
printf("%d\n",b[i]);
if(temp)
printf("The entered array is a palindrome");
else
printf("The entered array is not a palindrome");
getch();
return 0;
}

GCD of 2 numbers

Program to find the GCD of 2 numbers using the Euclid's algorithm.


#include<stdio.h>
#include<conio.h>
main()
{
int a,b,m,n,r;
clrscr();
printf("Enter the two nos\n");
scanf("%d%d",&a,&b);
if(a>b)
{
m=a;
n=b;
}
else
{
m=b;
n=a;
}
r=m%n;
while(r!=0)
{
m=n;
n=r;
r=m%n;
}
printf("GCD of %d and %d is %d",a,b,n);
getch();
return 0;
}

Bubble sort

Program to sort a given array using BUBBLE SORT TECHNIQUE..


#include&lt;stdio.h>
#include&lt;conio.h>
#include&lt;time.h>
#include&lt;dos.h>
#include&lt;stdlib.h>
main()
{
void bbl_sort(int *,int);
int n,i,a[20];
clrscr();
printf("\nEnter the no elements : ");
scanf("%d",&n);
printf("\nEnter %d elements\n",n);
for(i=0;i&lt;n;i++)
scanf("%d",&a[i]);
for(i=0;i&lt;1000;i++)
bbl_sort(a,n);
printf("\nSorting.... " );
getch();
printf("\n");
printf("\nSorted array is \n");
printf("\n");
for(i=0;i&lt;n;i++)
printf("%d\t",a[i]);
getch();
return 0;
}

void bbl_sort(int a[],int n)
{
int i,temp;
if(n==0)
return;
for(i=0;i&lt;n-1;i++)
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
bbl_sort(a,n-1);
}

Add Time

This is a program to two time entered in format hh:mm:ss..

#include<iostream.h>
#include<conio.h>
class time
{
int h,s,m;
public : void print();
friend time calc(time,time);
time()
{
h=0;
m=0;
s=0;
}
time(int hr,int mn,int sec)
{
h=hr;
m=mn;
s=sec;
}
};
time calc(time p,time q)
{
time t;
t.s=p.s+q.s;
t.m=t.s/60;
t.s=t.s%60;
t.m=p.m+q.m+t.m;
t.h=t.m/60;
t.m=t.m%60;
t.h=p.h+q.h+t.h;
return t;
}
void time::print()
{
cout<<h<<":"<<m<<":"<<s<<"\n";
}
main()
{
time t3;
time t1(2,56,34);
time t2(3,4,56);
clrscr();
t3=calc(t1,t2);
cout<<"Time 1 = ";t1.print();
cout<<"Time 2 = ";t2.print();
cout<<"Final time = ";
t3.print();
getch();
return 0;
}