sort strings

Program to sort strings

#include < stdio.h>
#include < conio.h>
#include < string.h>
char a[5][20],t[20];

void swap(int n);
main()
{
int i,j,m,k;
clrscr();
printf("\nEnter the strings\n");
for(i=0;i < 5;i++)
gets(a[i]);
for(m=0;m < 5;m++)
{
for(i=0;i < 4-m;i++)
{
j=0;k=0;
while(k==0)
{
if(a[i][j]>a[i+1][j])
swap(i);
k++;
if(a[i][j]==a[i+1][j])
{
k=0;
j++;
}
}
}}

printf("Sorted strings\n\n");
for(i=0;i < 5;i++)
puts(a[i]);
getch();
}

void swap(int n)
{
int i;
for(i=0;a[n][i]!='\0';i++)
t[i]=a[n][i];
t[i]='\0';
for(i=0;a[n+1][i]!='\0';i++)
a[n][i]=a[n+1][i];
a[n][i]='\0';
for(i=0;t[i]!='\0';i++)
a[n+1][i]=t[i];
a[n+1][i]='\0';
}



Determinant of Matrix

Program to find Determinant of Matrix

#include < stdio.h>
#include < conio.h>
int det(int n,int c,int r);
int a[10][10],b[10][10];
powr(int a);
void main()
{
int i,j,k,m,ans;
clrscr();
printf("Enter the order : ");
scanf("%d",&m);
printf("enter the matrix\n");
for(i=0;i < m;i++)
{
for(j=0;j < m;j++)
{
scanf("%d",&a[i][j]);
b[i][j]=a[i][j];
}
}
ans=det(m,0,0);
printf("answer=%d",ans);
getch();
}

int det(int n,int x,int y)
{
int ans,i,j,c[10][10],ii,jj,k;
ans=0;
for(i=0;i < n;i++)
{
for(j=0;j < n;j++)
c[i][j]=a[i][j];
}
if(n==1)
return(c[x][y]);

else
{
for(k=0;k < n;k++)
{
for(i=0,ii=0;i < n;i++,ii++)
{
for(j=0,jj=0;j < n;j++,jj++)
{
if(i==x)
i++;
if(j==k)
j++;
a[ii][jj]=c[i][j];
}}
ans=ans+powr(x+k)*c[x][k]*det(n-1,0,0);
}
return(ans);
}

}


powr(int a)
{
if(a%2==0)
return(1);
else
return(-1);
}

Convert between Bin,Oct,Dec and Hex form

Program to Convert between Bin,Oct,Dec and Hex form

#include < stdio.h>
#include < conio.h>
#include < string.h>
int conv(char c);
tobin(int a);
tooct(int b);
tohex(int a);
main()
{
int l,m,k,i,j,a;
char n[25];
clrscr();
printf("Enter the no followed by type(as 1001b,234d,654o,4AC6h etc) : ");
scanf("%s",n);
l=strlen(n);
switch(n[l-1])
{
case 'b':
j=2;
break;
case 'o':
j=8;
break;
case 'h':
j=16;
break;
case 'd':
j=10;
break;
default:
j=10;
l++;
break;
}
a=0;
m=1;
for(i=l-2;i>=0;i--)
{
k=conv(n[i]);
a=a+k*m;
m=m*j;
}
printf("\n\n Dec : %d",a);
tobin(a);
tooct(a);
tohex(a);
getch();
}
int conv(char a)
{
int p;
p=char(a);
if(p>47)
return(p-48);
else if(p>64)
return(p-55);
else if(p>96)
return(p-97);
}

tobin(int a)
{
char h[300];
int l,i,k;
for(i=0;a!=0;i++)
{
k=a%2;
h[i]=char(48+k);
a=a/2;
}
l=i-1;
printf("\n\n Bin : ");
for(i=l;i>=0;i--)
printf("%c",h[i]);

}

tooct(int a)
{
int b=0,k,m=1;
while(a!=0)
{
k=a%8;
b=b+m*k;
m=m*10;
a=a/8;
}
printf("\n\n Oct : %d",b);
}

tohex(int a)
{
char h[30];
int l,i,k;
for(i=0;a!=0;i++)
{
k=a%16;
if(k < 10)
h[i]=char(48+k);
else
h[i]=char(55+k);
a=a/16;
}
l=i-1;
printf("\n\n Hex : ");
for(i=l;i>=0;i--)
printf("%c",h[i]);
}

String-Replace

program to replace a string by another string

#include < stdio.h>
#include < conio.h>
#include < string.h>
main()
{
int i,j,mm,k=0,l3,l1,l2;
char a[200],b[50],c[50],ch;
clrscr();
printf("\nEnter the string : ");
gets(a);
printf("\nEnter the string to be deleted : ");
gets(b);
printf("\nReplace it with : ");
gets(c);
l1=strlen(a);
l2=strlen(b);
l3=strlen(c);
for(i=0;a[i]!='\0';i++)
{
if(b[0]==a[i])
{
k=1;
for(j=0;b[j]!='\0';j++)
{
if(a[i+j]!=b[j])
{
k--;
break;
}
}
if(b[j]=='\0')
{
printf("\nWord found do u want to replace it(y/n) : ");
ch=getche();
if(ch=='y'||ch=='Y')
{
for(j=i;j < =l1-l2;j++)
a[j]=a[j+l2];
l1=strlen(a);
for(mm=0;mm < l3;mm++)
for(j=l1+l3-1;j>i;j--)
a[j]=a[j-1];
for(j=0;j < l3;j++)
a[i+j]=c[j];
l1=strlen(a);
printf("\n\n%s",a);
}
}
}
}
if(k==0)
printf("Not found");
getch();
}C-Programming Standards & Guidelines: Version USearch Amazon.com for cprogramming

String -Delete

program to delete a substring from a string

#include < stdio.h>
#include < conio.h>
#include < string.h>
main()
{
int i,j,k=0,l1,l2;
char a[200],b[50],ch;
clrscr();
printf("\nEnter the string : ");
gets(a);
printf("\nEnter the string to be deleted : ");
gets(b);
l1=strlen(a);
l2=strlen(b);
for(i=0;a[i]!='\0';i++)
{
if(b[0]==a[i])
{
k=1;
for(j=0;b[j]!='\0';j++)
{
if(a[i+j]!=b[j])
{
k--;
break;
}
}
if(b[j]=='\0')
{
printf("\nWord found do u want to delete it(y/n) : ");
ch=getche();
if(ch=='y'||ch=='Y')
{
for(j=i;j < l1-l2;j++)
a[j]=a[j+l2];
printf("\n\n%s",a);
}
}
}
}
if(k==0)
printf("Not found");
getch();
}

String Search

Program to search a string

#include < stdio.h>
#include < conio.h>
#include < string.h>
main()
{
int i,j,k=0;
char a[200],b[50];
clrscr();
printf("\nEnter the string : ");
gets(a);
printf("\nEnter the string to be found : ");
gets(b);
for(i=0;a[i]!='\0';i++)
{
if(b[0]==a[i])
{
k=1;
for(j=0;b[j]!='\0';j++)
{
if(a[i+j]!=b[j])
{
k--;
break;
}
}}
if(k==1)
break;
}
if(k==1)
printf("Found");
else
printf("Not found");
getch();
}