• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

ProjectsGeek

Sparse Matrix Operations Code using C Langauge

April 3, 2011 by ProjectsGeek Leave a Comment

Sparse Matrix Operations  in C

Represent Sparse Matrix using array and perform Matrix Addition, Simple and Fast Transpose.
Polynomial representation using array, Concept of Sparse Matrix, it’s usage & representation using arrays, Algorithms for sparse matrix operations like addition, simple transpose, fast transpose & multiplication
 

 #define max 50  
 #include  
 #include  
 int input(int as[][3]);  
 void display(int*,int,int);  
 void display1(int[][3]);  
 void sp(int *,int [][3],int,int);  
 void add(int[][3],int[][3],int[][3]);  
 void sub(int[][3],int[][3],int[][3]);  
 void trans(int [][3],int [][3]);  
 void ftrans(int[][3],int [][3]);  
 void mul(int[][3],int [][3],int[][3]);  
 void main()  
 {  
 int as[max][3],bs[max][3],r[max][3],ch,ch1;  
 as[0][2]=0;  
 bs[0][2]=0 ;  
 do  
 {  
 clrscr();  
 printf("Enter your choice to performe sparse matrix menuplation");  
 printf("\n1.Input matrix\n2.Display Sparse matrix");  
 printf("\n3.Addition of two sparse matrix\n");  
 printf("4.Subtraction of two sparse matrix");  
 printf("\n5.Multiplecation of two sparse matrix");  
 printf("\n6.Transpose of matrix\n7.Fast transpose of matrix");  
 printf("\n8.Exit");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 do  
 {  
 clrscr();  
 printf("\nEnter your choice\n");  
 printf("1.Matrix 1\n2.Matrix 2\n3.return");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1 :  
 clrscr();  
 input(as);  
 break;  
 case 2 :  
 clrscr();  
 input(bs);  
 break;  
 case 3:  
 break;  
 default:  
 printf("\nInvalid choice\n");  
 getch();  
 }  
 }while(ch1!=3);  
 break;  
 case 2:  
 do  
 {  
 clrscr();  
 printf("\nEnter your choice to display\n");  
 printf("1.Matrix 1\n2.Matrix 2\n3.return");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1 :  
 clrscr();  
 if(as[0][2]==0)  
 {  
 printf("First enter the matrix1\n");  
 getch();  
 }  
 else  
 {  
 printf("Sparse matrix 1=\n");  
 display1(as);  
 getch();  
 }  
 break;  
 case 2 :  
 clrscr();  
 if(bs[0][2]==0)  
 {  
 printf("First enter the matrix2\n");  
 getch();  
 }  
 else  
 {  
 printf("Sparse matrix 2=\n");  
 display1(bs);  
 getch();  
 }  
 break;  
 case 3:  
 break;  
 default:  
 printf("\nInvalid choice\n");  
 getch();  
 }  
 }while(ch1!=3);  
 break;  
 case 3:  
 clrscr();  
 if(as[0][2]==0||bs[0][2]==0)  
 {  
 printf("First enter both matrices\n");  
 getch();  
 }  
 else  
 {  
 printf("SPARSE MATRIX A-\n");  
 display1(as);  
 printf("SPARSE MATRIX B-\n");  
 display1(bs);  
 add(as,bs,r);  
 printf("\nSPARSE ADDITION OF TWO MATRIX-\n");  
 display1(r);  
 getch();  
 }  
 break;  
 case 4:  
 do  
 {  
 clrscr();  
 printf("\nEnter your choice to Sparse subtraction");  
 printf("\n1.MAT A- MAT B\n2.MAT B-MAT A\n3.RETURN");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 clrscr();  
 if(as[0][2]==0||bs[0][2]==0)  
 {  
 printf("First enter both matrices\n");  
 getch();  
 }  
 else  
 {  
 printf("SPARSE MATRIX A-\n");  
 display1(as);  
 printf("SPARSE MATRIX B-\n");  
 display1(bs);  
 sub(as,bs,r);  
 printf("\nSPARSE SUBTRACTION OF TWO MATRIX-\n");  
 display1(r);  
 getch();  
 }  
 break;  
 case 2:  
 clrscr();  
 if(as[0][2]==0||bs[0][2]==0)  
 {  
 printf("First enter both matrices\n");  
 getch();  
 }  
 else  
 {  
 printf("SPARSE MATRIX A-\n");  
 display1(as);  
 printf("SPARSE MATRIX B-\n");  
 display1(bs);  
 sub(bs,as,r);  
 printf("\nSPARSE SUBTRACTION OF TWO MATRIX-\n");  
 display1(r);  
 getch();  
 }  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 5:  
 do  
 {  
 clrscr();  
 printf("\nEnter your choice to Sparse subtraction");  
 printf("\n1.MAT A* MAT B\n2.MAT B*MAT A\n3.RETURN");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 clrscr();  
 if(as[0][2]==0||bs[0][2]==0)  
 {  
 printf("First enter both matrices\n");  
 getch();  
 }  
 else  
 {  
 printf("SPARSE MATRIX A-\n");  
 display1(as);  
 printf("SPARSE MATRIX B-\n");  
 display1(bs);  
 mul(as,bs,r);  
 printf("\nSPARSE MULTIPLECATION OF TWO MATRIX-\n");  
 display1(r);  
 getch();  
 }  
 break;  
 case 2:  
 clrscr();  
 if(as[0][2]==0||bs[0][2]==0)  
 {  
 printf("First enter both matrices\n");  
 getch();  
 }  
 else  
 {  
 printf("SPARSE MATRIX A-\n");  
 display1(as);  
 printf("SPARSE MATRIX B-\n");  
 display1(bs);  
 mul(bs,as,r);  
 printf("\nSPARSE MULTIPLECATION OF TWO MATRIX-\n");  
 display1(r);  
 getch();  
 }  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 6:  
 do  
 {  
 clrscr();  
 printf("\nEnter your choice to Sparse Simple transpose");  
 printf("\n1.MAT A\n2.MAT B\n3.RETURN");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 clrscr();  
 if(as[0][2]==0)  
 {  
 printf("First enter the matrix1\n");  
 getch();  
 }  
 else  
 {  
 printf("SPARSE MATRIX A-\n");  
 display1(as);  
 trans(as,r);  
 printf("\nSPARSE SIMPLE TRANSPOSE OF TWO MATRIX-\n");  
 display1(r);  
 getch();  
 }  
 break;  
 case 2:  
 clrscr();  
 if(bs[0][2]==0)  
 {  
 printf("First enter the matrix2\n");  
 getch();  
 }  
 else  
 {  
 printf("SPARSE MATRIX B-\n");  
 display1(bs);  
 trans(bs,r);  
 printf("\nSPARSE SIMPLE TRANSPOSE OF TWO MATRIX-\n");  
 display1(r);  
 getch();  
 }  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 7:  
 clrscr();  
 do  
 {  
 clrscr();  
 printf("\nEnter your choice to Sparse Fast transpose");  
 printf("\n1.MAT A\n2.MAT B\n3.RETURN");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 clrscr();  
 if(as[0][2]==0)  
 {  
 printf("First enter the matrix1\n");  
 getch();  
 }  
 else  
 {  
 printf("SPARSE MATRIX A-\n");  
 display1(as);  
 ftrans(as,r);  
 printf("\nSPARSE FAST TRANSPOSE OF MATRIX A-\n");  
 display1(r);  
 getch();  
 }  
 break;  
 case 2:  
 clrscr();  
 if(bs[0][2]==0)  
 {  
 printf("First enter the matrix2\n");  
 getch();  
 }  
 else  
 {  
 printf("SPARSE MATRIX B-\n");  
 display1(bs);  
 ftrans(bs,r);  
 printf("\nSPARSE FAST TRANSPOSE OF MATRIX B-\n");  
 display1(r);  
 getch();  
 }  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 8:  
 break;  
 default:  
 printf("\nINVALID CHOICE");  
 getch();  
 }  
 }while(ch!=8);  
 getch();  
 }  
 int input(int as[][3])  
 {  
 int *mat,*r,*c,i,j,n1;  
 printf("Enter no. of rows <50:\n");  
 scanf("%d",r);  
 printf("Enter no. of colums <50:\n");  
 scanf("%d",c);  
 if(*r>max || *c>max)  
 {  
 printf("Error : \nSize of matrix is not is range.");  
 getch();  
 return(0);  
 }  
 else  
 {  
 mat=(int*)calloc(*r,(*c)*(sizeof(int)));  
 printf("Enter elements of matrix \n");  
 for(i=0;i<*r;i++)  
 {  
 for(j=0;j<*c;j++)  
 {  
 scanf("%d",(mat+(i*(*c))+j));  
 }  
 }  
 }  
 printf("Matrix is -\n");  
 display(mat,*r,*c);  
 printf("\nSparse form of matrix is-\n");  
 sp(mat,as,*r,*c);  
 free(mat);  
 display1(as);  
 printf("\n");  
 getch();  
 return(0);  
 }  
 void display(int *mat,int r,int c)  
 {  
 int i,j;  
 for(i=0;i<r;i++) <br=""> {  
 for(j=0;j<c;j++) <br=""> {  
 printf("%d\t",*(mat+(i*c)+j));  
 }  
 printf("\n");  
 }  
 }  
 void sp(int *mat,int as[][3], int r,int c)  
 {  
 int i,j,n=0;  
 as[0][0]=r;  
 as[0][1]=c;  
 for(i=0;i<r;i++) <br=""> {  
 for(j=0;j<c;j++) <br=""> {  
 if(*(mat+(i*c)+j)!=0)  
 {  
 n++;  
 as[n][0]=i;  
 as[n][1]=j;  
 as[n][2]=*(mat+(i*c)+j);  
 }  
 }  
 }  
 as[0][2]=n;  
 }  
 void display1(int mat[][3])  
 {  
 int i;  
 for(i=0;i<=mat[0][2];i++)  
 printf("%d\t%d\t%d\n",mat[i][0],mat[i][1],mat[i][2]);  
 }  
 void add(int as[][3],int bs[][3],int r[][3])  
 {  
 int i=1,j=1,k=1;  
 if(as[0][0]!=bs[0][0] || as[0][1]!=bs[0][1])  
 {  
 printf("\nRank of matrices is not Suitable");  
 return;  
 }  
 else  
 {  
 r[0][0]=as[0][0];  
 r[0][1]=as[0][1];  
 while(i<=as[0][2] && j<=bs[0][2])  
 {  
 if(as[i][0]==bs[j][0] && as[i][1]==bs[j][1])  
 {  
 r[k][2]=as[i][2]+bs[j][2];  
 if(r[k][2]!=0)  
 {  
 r[k][0]=as[i][0];  
 r[k][1]=as[i][1];  
 k++;  
 }  
 j++;i++;  
 }  
 else  
 {  
 if(as[i][0]<bs[j][0]) <br=""> {  
 r[k][0]=as[i][0];  
 r[k][1]=as[i][1];  
 r[k][2]=as[i][2];  
 i++;k++;  
 }  
 else  
 {  
 if(as[i][0]==bs[j][0])  
 {  
 if(as[i][1]<bs[j][1]) <br=""> {  
 r[k][0]=as[i][0];  
 r[k][1]=as[i][1];  
 r[k][2]=as[i][2];  
 i++;k++;  
 }  
 else  
 {  
 r[k][0]=bs[j][0];  
 r[k][1]=bs[j][1];  
 r[k][2]=bs[j][2];  
 j++;k++;  
 }  
 }  
 else  
 {  
 r[k][0]=bs[j][0];  
 r[k][1]=bs[j][1];  
 r[k][2]=bs[j][2];  
 j++;k++;  
 }  
 }  
 }  
 }  
 while(i<=as[0][2])  
 {  
 r[k][0]=as[i][0];  
 r[k][1]=as[i][1];  
 r[k][2]=as[i][2];  
 i++;k++;  
 }  
 while(j<=bs[0][2])  
 {  
 r[k][0]=bs[j][0];  
 r[k][1]=bs[j][1];  
 r[k][2]=bs[j][2];  
 j++;k++;  
 }  
 }  
 r[0][2]=k-1;  
 }  
 void sub(int as[][3],int bs[][3],int r[][3])  
 {  
 int i=1,j=1,k=1;  
 if(as[0][0]!=bs[0][0] || as[0][1]!=bs[0][1])  
 {  
 printf("\nRank of matrices is not Suitable");  
 return;  
 }  
 else  
 {  
 r[0][0]=as[0][0];  
 r[0][1]=as[0][1];  
 while(i<=as[0][2] && j<=bs[0][2])  
 {  
 if(as[i][0]==bs[j][0] && as[i][1]==bs[j][1])  
 {  
 r[k][2]=as[i][2]-bs[j][2];  
 if(r[k][2]!=0)  
 {  
 r[k][0]=as[i][0];  
 r[k][1]=as[i][1];  
 k++;  
 }  
 j++;i++;  
 }  
 else  
 {  
 if(as[i][0]<bs[j][0]) <br=""> {  
 r[k][0]=as[i][0];  
 r[k][1]=as[i][1];  
 r[k][2]=as[i][2];  
 i++;k++;  
 }  
 else  
 {  
 if(as[i][0]==bs[j][0])  
 {  
 if(as[i][1]<bs[j][1]) <br=""> {  
 r[k][0]=as[i][0];  
 r[k][1]=as[i][1];  
 r[k][2]=as[i][2];  
 i++;k++;  
 }  
 else  
 {  
 r[k][0]=bs[j][0];  
 r[k][1]=bs[j][1];  
 r[k][2]=-bs[j][2];  
 j++;k++;  
 }  
 }  
 else  
 {  
 r[k][0]=bs[j][0];  
 r[k][1]=bs[j][1];  
 r[k][2]=-bs[j][2];  
 j++;k++;  
 }  
 }  
 }  
 }  
 while(i<=as[0][2])  
 {  
 r[k][0]=as[i][0];  
 r[k][1]=as[i][1];  
 r[k][2]=as[i][2];  
 i++;k++;  
 }  
 while(j<=bs[0][2])  
 {  
 r[k][0]=bs[j][0];  
 r[k][1]=bs[j][1];  
 r[k][2]=-bs[j][2];  
 j++;k++;  
 }  
 }  
 r[0][2]=k-1;  
 }  
 void trans(int as[][3],int r[][3])  
 {  
 int i,j,k=1;  
 r[0][0]=as[0][1];  
 r[0][1]=as[0][0];  
 r[0][2]=as[0][2];  
 for(i=0;i<as[0][1];i++) <br=""> {  
 for(j=1;j<=as[0][2];j++)  
 {  
 if(as[j][1]==i)  
 {  
 r[k][0]=as[j][1];  
 r[k][1]=as[j][0];  
 r[k][2]=as[j][2];  
 k++;  
 }  
 }  
 }  
 }  
 void ftrans(int as[][3],int r[][3])  
 {  
 int i,j,count[max],pos[max],rp;  
 r[0][0]=as[0][1];  
 r[0][1]=as[0][0];  
 r[0][2]=as[0][2];  
 for(i=0;i<as[0][1];i++) <br=""> count[i]=0;  
 for(i=1;i<=as[i][2];i++)  
 count[as[i][1]]++;  
 pos[0]=1;  
 for(i=0;i<(as[0][1]-1);i++)  
 pos[i+1]=pos[i]+count[i];  
 for(i=1;i<=as[0][2];i++)  
 {  
 rp=pos[as[i][1]]++;  
 r[rp][0]=as[i][1];  
 r[rp][1]=as[i][0];  
 r[rp][2]=as[i][2];  
 }  
 }  
 void mul(int as[][3],int bs[][3],int r[][3])  
 {  
 int i,j,*t;  
 t=(int*)calloc(as[0][0],(bs[0][1])*(sizeof(int)));  
 if(as[0][1]==bs[0][0])  
 {  
 for(i=1;i<=as[0][2];i++)  
 {  
 for(j=1;j<=bs[0][2];j++)  
 {  
 if(as[i][1]==bs[j][0])  
 {  
 *(t+(as[i][0]*bs[0][1])+bs[j][1])+=as[i][2]*bs[j][2];  
 }  
 }  
 }  
 }  
 else  
 printf("\nYOUR MATRICES ARE NOT SUITABLE FOR MULTIPLECTION");  
 sp(t,r,as[0][0],bs[0][1]);  
 free(t);  
 }

 

Other Projects to Try:

  1. Matrix operations in c language
  2. Matrix Operations in C Language
  3. Matrix Operations with Pointers
  4. Operations on matrices like addition, multiplication, saddle point, magic square ,inverse & transpose
  5. Hash table code in C Language

Filed Under: C Assignments, Fundamentals of Datastructure Tagged With: Datastructure Assignments

Double Link List code using C Langauge

April 3, 2011 by ProjectsGeek Leave a Comment

Implement Double Link List operations

 

Implement a Double Link list using C language to perform basic operations that can be performed on Double Link List . Some of the Basic Double Link list operations that can be implemented on Double Link List are Given Below ….

  • Create Double Link List
  • Display Double Link list
  • Delete Node
  • Insert Node

Double Link list assignment is implemented as Fundamentals of Data structure(FDS) as Second Year (SE-IT) Assignment under Pune University and Mumbai University .

Double Link list Code

#include  
 #include  
 typedef struct link  
 {  
 char c;  
 struct link *lptr,*rptr;  
 }dll;  
 dll* alloc(char ch)  
 {  
 dll* f;  
 f=(dll*)malloc(sizeof(dll));  
 f->rptr=NULL;  
 f->lptr=NULL;  
 f->c=ch;  
 return f;  
 }  
 dll* freeall(dll *f)  
 {  
 dll* temp;  
 while(f!=NULL)  
 {  
 temp=f;  
 f=f->rptr;  
 f->rptr->lptr=NULL;  
 free(temp);  
 }  
 return f;  
 }  
 dll* create()  
 {  
  dll *f,*nw;  
  char x;  
  f=NULL;  
  printf("\n Enter a string :");  
  flushall();  
  while((x=getchar())!='\n')  
   {  
 if(f==NULL)  
 {  
  nw=alloc(x);  
  f=nw;  
 }  
 else  
  {  
  nw->rptr=alloc(x);  
  nw->rptr->lptr=nw;  
  nw=nw->rptr;  
  nw->rptr=NULL;  
  }  
   }  
  return(f);  
 }  
 void reverse(dll *f)  
 {  
 dll *p;  
 p=f;  
 if(p==NULL)  
 {  
 printf("\n Empty list");  
 return;  
 }  
 printf("\n\nInfo\tleft ptr\taddress\t\tright ptr");  
 while(p->rptr!=NULL)  
 p=p->rptr;  
 printf("\n%c\t%d\t\t%d\t\t%d",p->c,p->lptr,p,p->rptr);  
 do  
 {  
 p=p->lptr;  
 printf("\n%c\t%d\t\t%d\t\t%d",p->c,p->lptr,p,p->rptr);  
 }while(p!=f);  
 }  
 //DISPLAYING THE LINKED LIST  
 void display(dll *f)  
 {  
 dll *p;  
 p=f;  
 if(p==NULL)  
 {  
 printf("\n Empty list");  
 return;  
 }  
 printf("\n\nInfo\tleft ptr\taddress\t\tright ptr");  
 while(p!=NULL)  
 {  
 printf("\n%c\t%d\t\t%d\t\t%d",p->c,p->lptr,p,p->rptr);  
 p=p->rptr;  
 }  
 }  
 dll* del(dll *f,char ch)  
 {  
 dll *temp;  
 temp=f;  
 while((temp!=NULL)&&(temp->c!=ch))  
 temp=temp->rptr;  
 if(temp==NULL)  
 {  
 printf("THE CHARACTER IS NOT FOUND");  
 return f;  
 }  
 if(temp!=f)  
 temp->lptr->rptr=temp->rptr;  
 else  
 f=temp->rptr;  
 if(temp->rptr!=NULL)  
 temp->rptr->lptr=temp->lptr;  
 free(temp);  
 printf("THE CHARACTER HAS BEEN DELETED");  
 return f;  
 }  
 void insert_a(dll*f,char ch)  
 {  
 dll *nw;  
 char a;  
 while((f!=NULL)&&(f->c!=ch))  
 f=f->rptr;  
 if(f==NULL)  
 printf("VALUE NOT FOUND");  
 else  
 {  
 printf("ENTER CHARACTER TO BE INSERTED");  
 flushall();  
 scanf("%c",&a);  
 nw=alloc(a);  
 nw->lptr=f;  
 nw->rptr=f->rptr;  
 f->rptr=nw;  
 if(nw->rptr!=NULL)  
 nw->rptr->lptr=nw;  
 }  
 }  
 dll* insert_b(dll *f,char ch)  
 {  
 dll *nw,*temp;  
 char a;  
 temp=f;  
 while((f!=NULL)&&(f->c!=ch))  
 f=f->rptr;  
 if(f==NULL)  
 {  
 printf("VALUE NOT FOUND");  
 return f;  
 }  
 printf("ENTER CHARACTER TO BE INSERTED");  
 flushall();  
 scanf("%c",&a);  
 nw=alloc(a);  
 nw->rptr=f;  
 nw->lptr=f->lptr;  
 f->lptr=nw;  
 if(nw->lptr!=NULL)  
 nw->lptr->rptr=nw;  
 else  
 temp=nw;  
 return temp;  
 }  
 void main()  
 {  
 dll *f;  
 int ch,i;  
 char c;  
 do  
 {  
 clrscr();  
 printf("\n\t\t\t\tMAIN MENU");  
 printf("\n\t\t\t1.Create\n\t\t\t2.Display\n\t\t\t3.Delete a node");  
 printf("\n\t\t\t4.Insert a node\n\t\t\t5.Display backwards \n\t\t\t6.Exit");  
 printf("\n\nEnter your choice : ");  
 flushall();  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 f=create();  
 printf("THE LIST HAS BEEN CREATED");  
 getch();  
 break;  
 case 2:  
 display(f);  
 getch();  
 break;  
 case 3:  
 printf("ENTER THE CHARACTER TO BE DELETED");  
 flushall();  
 scanf("%c",&c);  
 f=del(f,c);  
 getch();  
 break;  
 case 4: do  
 {  
 clrscr();  
 printf("\n\t\t\t\tINSERT\n");  
 printf("\n\t\t\t1.AFTER A CHARACTER\n");  
 printf("\n\t\t\t2.BEFORE A CHARACTER\n");  
 printf("\n\t\t\t3.EXIT\n");  
 printf("ENTER YOUR CHOICE : ");  
 flushall();  
 scanf("%d",&ch);  
 if(ch==1||ch==2)  
 {  
 printf("Enter that character : ");  
 flushall();  
 scanf("%c",&c);  
 }  
 switch(ch)  
 {  
 case 1: insert_a(f,c);  
 printf("\nThe value is inserted\n");  
 getch();  
 break;  
 case 2: f=insert_b(f,c);  
 printf("\nThe value is inserted\n");  
 getch();  
 break;  
 case 3:  
 break;  
 default:  
 printf("Invalid,enter again\n");  
 getch();  
 }  
 }while(ch!=3);  
 break;  
 case 5:  
 reverse(f);  
 getch();  
 break;  
 case 6:  
 break;  
 default:  
 printf("Invalid choice");  
 getch();  
 }  
 }while(ch!=6);  
 }

Download Source Code

 

Other Projects to Try:

  1. Single Link List code using C Language
  2. Circular Link List using C Language
  3. Sparse Matrix Operations Code using C Langauge
  4. String Operations with Pointers
  5. string operations such as Copy, Length, Reversing, Palindrome, Concatenation

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

Single Link List code using C Language

April 3, 2011 by ProjectsGeek Leave a Comment

Perform the Implementation of Single Link List

 

Source Code for Single Link list in C and C++ Language with the following functions ::

  • Create Single Link list
  • Display Single Link list
  • Insert Node
  • Delete node
  • Reverse Single Link list
  • Revert

Single link list Assignment is implemented using pointers with every function is divided modular to increase codes readability .

Single Link List C Language code

 

#include
#include

typedef struct node

{

int info;

struct node *lnk;

}node;

node* create();

node* alloc();

void display(node *);

node* insert(node *,int);

node* insert1(node *,int);

node* dlt(node *,int);

node* reverse(node *);

void main()

{

int ch,n,a[10],key,ch1;

node *f,*f1,*f2;

do

{

clrscr();

printf("\t\t\tLINKED LIST MAIN MENU\n");

printf("\t\t\t1:CREATE\n");

printf("\t\t\t2:DISPLAY\n");

printf("\t\t\t3:INSERT\n");

printf("\t\t\t4:DELETE\n");

printf("\t\t\t5:REVERSE\n");

printf("\t\t\t6:REVERT\n");

printf("\t\t\t7:EXIT\n");

printf("\t\tENTER YOUR CHOICE\n");

scanf("%d",&ch);

switch(ch)

{

case 1:clrscr();

f=create();

getch();

break;

case 2:clrscr();

printf("\n\t\tDISPLAYING THE LINKED LIST\n\n");

display(f);

getch();

break;

case 3:clrscr();

do

{

clrscr();

printf("\n\t\tINSERT MENU");

printf("\n\t\t1.BEFORE A NO\n\t\t2.AFTER A NO\n\t\t3.EXIT");

scanf("%d",&ch1);

switch(ch1)

{

case 1: clrscr();

printf("ENTER THE ELEMENT OF THE LIST");

scanf("%d",&key);

f=insert(f,key);

display(f);

getch();

break;

case 2: clrscr();

printf("ENTER THE ELEMENT OF THE LIST");

scanf("%d",&key);

f1=insert1(f,key);

display(f1);

getch();

break;

case 3:

break;

}

}while(ch1!=3);

break;

case 4: clrscr();

printf("ENTER THE ELEMENT OF THE LIST");

scanf("%d",&key);

f1=dlt(f,key);

display(f1);

getch();

break;

case 5: f2=reverse(f);

display(f2);

f2=reverse(f2);

getch();

break;

case 6: f=reverse(f);

display(f);

printf("\n\t\tLIST REVERSED\t\t");

getch();

break;

case 7:

break;

default:

printf("WRONG CHOICE !!!! RE ENTER");

getch();

break;

}

}while(ch!=7);

}

node* create()

{

node *f,*nw,*lst;

f=NULL;

do

{

nw=alloc();

if(f==NULL)

f=nw;

else

lst->lnk=nw;

lst=nw;

printf("\n\n\tDO YOU WANT TO ENTER ANOTHER DATABASE(y/n)?");

}while(getche()=='y');

return f;

}

node* alloc()

{

node *f;

f=(node *)malloc(sizeof(node));

f->lnk=NULL;

printf("\n\tENTER THE ELEMENT TO BE ADDED");

scanf("%d",&f->info);

return f;

}

void display(node *f)

{

if(f==NULL)

{

printf("\n\n\t\tLIST NOT PRESENT !!!!");

return;

}

printf("\n\tINFO\tRPTR\tADD\n");

while(f!=NULL)

{

printf("\t%d\t%d\t%d\n",f->info,f->lnk,f);

f=f->lnk;

}

}

node* insert(node *f,int key)

{

node *nw,*t;

if(f->info==key)

{

nw=alloc();

nw->lnk=f;

f=nw;

return f;

}

while(f->lnk!=NULL)

{

if(f->lnk->info==key)

{

nw=alloc();

nw->lnk=f->lnk;

f->lnk=nw;

return f;

}

f=f->lnk;

}

printf("\n\t\t\tELEMENT NOT FOUND !!!!");

return f;

}

node* insert1(node *f,int key)

{

node *nw,*p;

f=p;

while(p!=NULL)

{

if(key==p->info)

{

nw=alloc();

nw->lnk=p->lnk;

p->lnk=nw;

return f;

}

p=p->lnk;

}

printf("element not found");

}

node* dlt(node *f,int key)

{

node *p,*q;

p=f;

if(f==NULL)

{

printf("\n\tLINKED LIST EMPTY");

return f;

}

if(key==f->info)

{

f=f->lnk;

free(p);

return f;

}

else

{

while(p->lnk!=NULL)

{

if(key==(p->lnk)->info)

{

q=p->lnk;

p->lnk=p->lnk->lnk;

free(q);

}

p=p->lnk;

}

return f;

}

printf("ELEMENT NOT FOUND");

}

node* reverse(node *f)

{

node *p,*t,*r;

p=NULL;

t=f;

r=t->lnk;

while(t!=NULL)

{

t->lnk=p;

p=t;

t=r;

if(t!=NULL)

r=r->lnk;

}

return(p);

}

Download Source Code

Other Projects to Try:

  1. Double Link List code using C Langauge
  2. Circular Link List using C Language
  3. Hash table code in C Language
  4. Sparse Matrix Operations Code using C Langauge
  5. BFS AND DFS Algorithm using C Language

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

Simple Student Database using C Language

April 3, 2011 by ProjectsGeek 2 Comments

Simple Student Database using C Language

Implement a C Code to implement Student Database to some basic Student data . Data which we can store in Student database is as Given below :

  • Create a Database
  • Insert Inside a Database
  • Display data
  • Edit Database Data
  • Search Database
  • Delete Database

All options are Implemented with pointers as well as without pointers as Fundamentals of Data structure assignments under Pune University and Mumbai University .

Simple Student Database Code

 #include  
 #include  
 #include "c:\tc\valid.c"  
 #define MAX 25  

 enum e{ co=1,dsgt,hss }; 

 struct add  
 {  
 char addr[50];  
 char city[25];  
 char state[25];  
 };  

 struct stud  
 {  
 int roll,fds,dem;  
 int a;  
 char name[50];  
 struct add add1;

 union elective  
 {  
 int co,dsgt,hss;  
 }elec;  
 };  
 int withp(struct stud [],int);  
 struct stud* insertp();  
 int createp (struct stud *,int);  
 int searchp(struct stud *,int);  
 void editp(struct stud *,int);  
 void deletp(struct stud *,int);  
 void displayp(struct stud*);  
 int withoutp(struct stud [],int);  
 struct stud insert();  
 int create (struct stud [],int);  
 int search(struct stud [],int);  
 void edit(struct stud s1[],int n);  
 void delet(struct stud s1[],int n);  
 void display(struct stud s1);  
 int check(struct stud[],int,int);  
 void main()  
 {  
 int ch,n=0;  
 struct stud st[25];  
 do  
 {  
 clrscr();  
 printf("\n\n\n\t\t>>>>----  MANAGING DATABASE  ----<<<<\n");  
 printf("\n\t1\tWITH POINTERS\n");  
 printf("\t2\tWITHOUT POINTERS\n");  
 printf("\t3\tEXIT\n\n");  
 printf("\n\t\tENTER UR CHOICE\t");  
 ch=valid();  
 switch(ch)  
 {  
 case 1:     n=withp(st,n);  
 break;  
 case 2:  
 n=withoutp(st,n);  
 break;  
 case 3:  
 break;  
 default:  
 printf("\n\n!!!!  INVALID CHOICE  !!!!");  
 getch();  
 }  
 }  
 while(ch!=3);  
 }//end of main  
 /*********************************************************************  
 WITHP FUNCTION  
 **********************************************************************/  
 int withp(struct stud s1[],int n)  
 {  
 int ch,i,m;  
 struct stud *s;  
 void (*p) (struct stud*);  
 p=displayp;  
 do  
 {  
 clrscr();  
 printf("\n\n\t\t>>>>----  WITHOUT POINTER MENU  ----<<<<\n");  
 printf("\n\t1\tCREATE A NEW DATABASE");  
 printf("\n\t2\tINSERT");  
 printf("\n\t3\tDISPLAY");  
 printf("\n\t4\tEDIT");  
 printf("\n\t5\tSEARCH");  
 printf("\n\t6\tDELETE");  
 printf("\n\t7\tRETURN TO MAIN MENU \n");  
 printf("\n\t\tENTER UR CHOICE\t");  
 ch=valid();  
 switch(ch)  
 {  
 case 1:  
 n=createp(s1,MAX);  
 break;  
 case 2:  
 m=0;  
 do  
 {  
 if(m!=0)  
 {  
 printf("\n\n\tENTER AGAIN!!!!");  
 getch();  
 }  
 s=insertp();  
 s1[n]=*s;  
 m++;  
 }while(check(s1,s1[n].roll,n)==1);  
 n++;  
 break;  
 case 3:  
 if(n==0)  
 {  
 printf("\n\tFIRST CREATE DATABADE!!!");  
 getch();  
 break;  
 }  
 for(i=0;i<n;i++) <br=""> p(&s1[i]);  
 break;  
 case 4:  
 if(n==0)  
 {  
 printf("\n\tFIRST CREATE DATABASE!!!");  
 getch();  
 break;  
 }  
 editp(s1,n);  
 break;  
 case 5: if(n==0)  
 {  
 printf("\n\tFIRST CREATE DATABASE!!!");  
 getch();  
 break;  
 }  
 i=searchp(s1,n);  
 if(i>=0)  
 { printf("\n\tTHE RECORD IS PRESENT");  
 getch();  
 p(&s1[i]);  
 }  
 else  
 { printf("\n\t!! NOT FOUND !!");  
 getch();  
 }  
 break;  
 case 6:  
 if(n==0)  
 {  
 printf("\n\tFIRST CREATE DATABASE!!!");  
 getch();  
 break;  
 }  
 deletp(s1,n);  
 n--;  
 break;  
 case 7:  
 break;  
 default:  
 printf("\n\n\t\t!!!!   INVALID CHOICE  !!!!");  
 getch();  
 }//end of switch  
 }while(ch!=7);  
 return n;  
 }  
 /**********************************************************  
 DISPLAYP FUNCTION  
 ***********************************************************/  
 void displayp(struct stud *s1)  
 {  
 clrscr();  
 printf("\n\n\tROLL NO : %d",s1->roll);  
 printf("\n\tNAME : %s",s1->name);  
 printf("\n\tADDRESS:\n\t\t%s  ,\n\t\t%s  ,\n\t\t%s",s1->add1.addr,s1->add1.city,s1->add1.state);  
 printf("\n\tMARKS:-\n\tFDS\t%d\n\tDEM\t%d",s1->fds,s1->dem);  
 if(s1->a==co)  
 printf("\n\tCO\t%d",s1->elec.co);  
 else if(s1->a==dsgt)  
 printf("\n\tDSGT\t%d",s1->elec.dsgt);  
 else  
 printf("\n\tHSS\t%d",s1->elec.hss);  
 printf("\n\n\t!!! PRESS ANY KEY TO CONTINUE !!!!");  
 getch();  
 }  
 /*********************************************************  
 INSERTP FUNCTION  
 **********************************************************/  
 struct stud* insertp()  
 {  
 struct stud *s1;  
 int m;  
 clrscr();  
 printf("\n\t!!! --- !!!! \n\t ENTER DETAILS\n");  
 printf("\n>>>  ROLL NO->\t");  
 do  
 {  
 m=valid();  
 }while(m==-1);  
 s1->roll=m;  
 printf("\n>>>  NAME\t");  
 flushall();  
 scanf("%s",s1->name);  
 printf("\n>>>  ADDRESS \n\tSTREET NO-> / ROOM NO->\t");  
 flushall();  
 gets(s1->add1.addr);  
 printf("\n\tCITY\t");  
 flushall();  
 gets(s1->add1.city);  
 printf("\n\tSTATE\t");  
 flushall();  
 gets(s1->add1.state);  
 printf("\n>>>  MARKS IN ");  
 printf("\n\tF D S\t");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1 || m>100);  
 s1->fds=m;  
 printf("\n\tDEM\t");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1|| m>100);  
 s1->dem=m;  
 printf("\n\tSELECT ELECTIVE SUBJECT\t");  
 printf("\n\t1\tCO");  
 printf("\n\t2\tDSGT");  
 printf("\n\t3\tHSS");  
 printf("\n\n\tENTER UR CHOICE\t");  
 m=0;  
 do  
 {  
 if(m!=0 || m>3)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1|| m>3);  
 s1->a=m;  
 if(s1->a==co)  
 {  
 printf("\n\tENTER MARKS IN CO\t ");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1|| m>100);  
 s1->elec.co=m;  
 }  
 else if(s1->a==dsgt)  
 {  
 printf("\n\tENTER MARKS IN DSGT\t ");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1|| m>100);  
 s1->elec.dsgt=m;  
 }  
 else  
 {  
 printf("\n\tENTER MARKS IN HSS\t");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1 || m>100) ;  
 s1->elec.hss=m;  
 }  
 return s1;  
 }  
 /*****************************************************  
 CREATEP FUNCTION  
 *******************************************************/  
 int createp(struct stud *s1,int n)  
 {  
 int i=0;  
 do  
 {  
 *(s1+i)=insert();  
 i++;  
 printf("\n\t!!! DO U WANT TO INSERT MORE RECORDS (y \ n) !!!");  
 }while(getche() =='y'&& i<=n);  
 return i;  
 }  
 /**********************************************************  
 SEARCHP FUNCTION  
 ********************************************************/  
 int searchp(struct stud *s1,int n)  
 {  
 int i=0,rno;  
 printf("\n\tENTER ROLL NUMBER\t");  
 scanf("%d",&rno);  
 for(i=0;i<n;i++) <br=""> {  
 if((s1+i)->roll==rno)  
 return i;  
 }  
 return -1;  
 }  
 /*******************************************************  
 EDITP FUNCTION  
 ********************************************************/  
 void editp(struct stud s1[],int n)  
 {  
 int i;  
 i=search(s1,n);  
 if(i>=0)  
 {  
 int ch;  
 do  
 {  
 printf("\n\t\t\t\tMODIFY\n");  
 printf("\n\t\t\t1.ROLL\n\t\t\t2.NAME\n\t\t\t3.ADDRESS\n\t\t\t4.MARKS\n\t\t\t5.RETURN");  
 printf("\nEnter your choice: ");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 printf("\nEnter new roll no: ");  
 scanf("%d",&s1[i].roll);  
 break;  
 case 2:  
 printf("\nEnter new name: ");  
 scanf("%s",&s1[i].name);  
 break;  
 case 3:  
 printf("\nEnter new address: ");  
 printf("\nHouse no. : ");  
 scanf("%d",&s1[i].add1.addr);  
 printf("\nCity: ");  
 scanf("%s",&s1[i].add1.city);  
 printf("\nState: ");  
 scanf("%s",&s1[i].add1.state);  
 break;  
 case 4:  
 printf("\nEnter new marks:");  
 printf("\nFDS: %d",s1[i].fds);  
 printf("\nDEM: %d",s1[i].dem);  
 if(s1[i].a==dsgt)  
 {  
 printf("\nDSGT: ");  
 scanf("%d",&s1[i].elec.dsgt);  
 }  
 if(s1[i].a==hss)  
 {  
 printf("\nHSS: ");  
 scanf("%d",&s1[i].elec.hss);  
 }  
 if(s1[i].a==co)  
 {  
 printf("\nCO: ");  
 scanf("%d",&s1[i].elec.co);  
 }  
 break;  
 case 5:  
 break;  
 default:printf("\n\nInvalid choice!!\n");  
 break;  
 }  
 }while(ch!=5);  
 printf("\n\n\tRECORD EDITED");  
 getch();  
 }  
 else  
 {  
 printf("\n\t\tRECORD NOT FOUND");  
 getch();  
 }  
 }  
 /*****************************************************  
 DELETP FUNCTION  
 *******************************************************/  
 void deletp(struct stud *s1,int n)  
 {  
 int i,j;  
 i=search(s1,n);  
 printf("\n\tdo u wanna delete\y(y\n)");  
 if(i>=0&&getche()=='y')  
 {  
 for(j=i+1;j<n;j++) <br=""> *(s1+j-1)=*(s1+j);  
 printf("\n\n\t!!!  DELETED !!!");  
 getch();  
 }  
 else  
 {  
 printf("\n\t\tnot deleted!!!!!!!!!");  
 getch();  
 }  
 }  
 /*********************************************************************  
 WITHOUTP FUNCTION  
 **********************************************************************/  
 int withoutp(struct stud s1[],int n)  
 {  
 int ch,i,m;  
 void (*p) (struct stud);  
 p=display;  
 do  
 {  
 clrscr();  
 printf("\n\n\t\t>>>>----  WITHOUT POINTER MENU  ----<<<<\n");  
 printf("\n\t1\tCREATE A NEW DATABASE");  
 printf("\n\t2\tINSERT");  
 printf("\n\t3\tDISPLAY");  
 printf("\n\t4\tEDIT");  
 printf("\n\t5\tSEARCH");  
 printf("\n\t6\tDELETE");  
 printf("\n\t7\tRETURN TO MAIN MENU \n");  
 printf("\n\t\tENTER UR CHOICE\t");  
 ch=valid();;  
 switch(ch)  
 {  
 case 1:  
 n=create(s1,MAX);  
 break;  
 case 2:  
 m=0;  
 do  
 {  
 if(m!=0)  
 {  
 printf("\n\n\tENTER AGAIN!!!!");  
 getch();  
 }  
 s1[n]=insert();  
 m++;  
 }while(check(s1,s1[n].roll,n)==1);  
 n++;  
 break;  
 case 3: if(n==0)  
 {  
 printf("\n\tFIRST CREATE DATABADE!!!");  
 getch();  
 break;  
 }  
 for(i=0;i<n;i++) <br=""> p(s1[i]);  
 break;  
 case 4:  
 if(n==0)  
 {  
 printf("\n\tFIRST CREATE DATABADE!!!");  
 getch();  
 break;  
 }  
 edit(s1,n);  
 break;  
 case 5:  
 if(n==0)  
 {  
 printf("\n\tFIRST CREATE DATABADE!!!");  
 getch();  
 break;  
 }  
 i=search(s1,n);  
 if(i>=0)  
 { printf("\n\tTHE RECORD IS PRESENT");  
 getch();  
 p(s1[i]);  
 }  
 else  
 { printf("\n\t!! NOT FOUND !!");  
 getch();  
 }  
 break;  
 case 6: if(n==0)  
 {  
 printf("\n\tFIRST CREATE DATABADE!!!");  
 getch();  
 break;  
 }  
 delet(s1,n);  
 n--;  
 break;  
 case 7:  
 break;  
 default:  
 printf("\n\n\t\t!!!!   INVALID CHOICE  !!!!");  
 getch();  
 }//end of switch  
 }while(ch!=7);  
 return n;  
 }  
 /**********************************************************  
 DISPLAY FUNCTION  
 ***********************************************************/  
 void display(struct stud s1)  
 {  
 clrscr();  
 printf("\n\n\tROLL NO : %d",s1.roll);  
 printf("\n\tNAME : %s",s1.name);  
 printf("\n\tADDRESS:\n\t\t%s  ,\n\t\t%s  ,\n\t\t%s",s1.add1.addr,s1.add1.city,s1.add1.state);  
 printf("\n\tMARKS:-\n\tFDS\t%d\n\tDEM\t%d",s1.fds,s1.dem);  
 if(s1.a==co)  
 printf("\n\tCO\t%d",s1.elec.co);  
 else if(s1.a==dsgt)  
 printf("\n\tDSGT\t%d",s1.elec.dsgt);  
 else  
 printf("\n\tHSS\t%d",s1.elec.hss);  
 printf("\n\n\t!!! PRESS ANY KEY TO CONTINUE !!!!");  
 getch();  
 }  
 /*********************************************************  
 INSERT FUNCTION  
 **********************************************************/  
 struct stud insert()  
 {  
 struct stud s1;  
 int m;  
 clrscr();  
 printf("\n\t!!! --- !!!! \n\t ENTER DETAILS\n");  
 printf("\n>>>  ROLL NO.\t");  
 m=0;  
 do  
 {  
 if(m!=0)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1);  
 s1.roll=m;  
 printf("\n>>>  NAME\t");  
 flushall();  
 scanf("%s",s1.name);  
 printf("\n>>>  ADDRESS \n\tSTREET NO. / ROOM NO.\t");  
 flushall();  
 gets(s1.add1.addr);  
 printf("\n\tCITY\t");  
 flushall();  
 gets(s1.add1.city);  
 printf("\n\tSTATE\t");  
 flushall();  
 gets(s1.add1.state);  
 printf("\n>>>  MARKS IN ");  
 printf("\n\tF D S\t");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1|| m>100);  
 s1.fds=m;  
 printf("\n\tDEM\t");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1|| m>100);  
 s1.dem=m;  
 printf("\n\tSELECT ELECTIVE SUBJECT\t");  
 printf("\n\t1\tCO");  
 printf("\n\t2\tDSGT");  
 printf("\n\t3\tHSS");  
 printf("\n\n\tENTER UR CHOICE\t");  
 m=0;  
 do  
 {  
 if(m!=0 || m>3)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1 || m>3);  
 s1.a=m;  
 if(s1.a==co)  
 {  
 printf("\n\tENTER MARKS IN CO\t ");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1 || m>100);  
 s1.elec.co=m;  
 }  
 else if(s1.a==dsgt)  
 {  
 printf("\n\tENTER MARKS IN DSGT\t ");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1 || m>100);  
 s1.elec.dsgt=m;  
 }  
 else  
 {  
 printf("\n\tENTER MARKS IN HSS\t");  
 m=0;  
 do  
 {  
 if(m!=0 || m>100)  
 printf("\n\t ENTER AGAIN !!!!!\n");  
 m=valid();  
 }while(m==-1 || m>100);  
 s1.elec.hss=m;  
 }  
 return s1;  
 }  
 /*****************************************************  
 CREATE FUNCTION  
 *******************************************************/  
 int create(struct stud s1[25],int n)  
 {  
 int i=0;  
 do  
 {  
 s1[i]=insert();  
 i++;  
 printf("\n\t!!! DO U WANT TO INSERT MORE RECORDS (y \ n) !!!");  
 }while(getche()=='y'&& i<=n);  
 return i;  
 }  
 /**********************************************************  
 SEARCH FUNCTION  
 ********************************************************/  
 int search(struct stud s1[],int n)  
 {  
 int i,rno;  
 printf("\n\tENTER ROLL NUMBER\t");  
 scanf("%d",&rno);  
 for(i=0;i<n;i++) <br=""> {  
 if(s1[i].roll==rno)  
 return i;  
 }  
 return -1;  
 }  
 /*******************************************************  
 EDIT FUNCTION  
 ********************************************************/  
 void edit(struct stud s1[],int n)  
 {  
 int i;  
 i=search(s1,n);  
 if(i>=0)  
 {  
 int ch;  
 do  
 {  
 printf("\n\t\t\t\tMODIFY\n");  
 printf("\n\t\t\t1.ROLL\n\t\t\t2.NAME\n\t\t\t3.ADDRESS\n\t\t\t4.MARKS\n\t\t\t5.RETURN");  
 printf("\nEnter your choice: ");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 printf("\nEnter new roll no: ");  
 scanf("%d",&s1[i].roll);  
 break;  
 case 2:  
 printf("\nEnter new name: ");  
 scanf("%s",&s1[i].name);  
 break;  
 case 3:  
 printf("\nEnter new address: ");  
 printf("\nHouse no. : ");  
 scanf("%d",&s1[i].add1.addr);  
 printf("\nCity: ");  
 scanf("%s",&s1[i].add1.city);  
 printf("\nState: ");  
 scanf("%s",&s1[i].add1.state);  
 break;  
 case 4:  
 printf("\nEnter new marks:");  
 printf("\nFDS: %d",s1[i].fds);  
 printf("\nDEM: %d",s1[i].dem);  
 if(s1[i].a==dsgt)  
 {  
 printf("\nDSGT: ");  
 scanf("%d",&s1[i].elec.dsgt);  
 }  
 if(s1[i].a==hss)  
 {  
 printf("\nHSS: ");  
 scanf("%d",&s1[i].elec.hss);  
 }  
 if(s1[i].a==co)  
 {  
 printf("\nCO: ");  
 scanf("%d",&s1[i].elec.co);  
 }  
 break;  
 case 5:  
 break;  
 default:printf("\n\nInvalid choice!!\n");  
 break;  
 }  
 }while(ch!=5);  
 printf("\n\n\tRECORD EDITED");  
 getch();  
 }  
 else  
 {  
 printf("\n\t\tRECORD NOT FOUND");  
 getch();  
 }  
 }  
 /*****************************************************  
 DELET FUNCTION  
 *******************************************************/  
 void delet(struct stud s1[],int n)  
 {  
 int i,j;  
 i=search(s1,n);  
 printf("\n\tdo u wanna delete\y(y\n)");  
 if(i>=0&&getche()=='y')  
 {  
 for(j=i+1;j<n;j++) <br=""> s1[j-1]=s1[j];  
 printf("\n\n\t!!!  DELETED !!!");  
 getch();  
 }  
 else  
 {  
 printf("\n\t\tRECORD NOT Deleted");  
 getch();  
 }  
 }  
 int check(struct stud e[],int rno, int n)  
 {  
 int i;  
 for(i=0;i<n;i++) <br=""> if(e[i].roll==rno)  
 return 1;  
 return 0;  
 }

 

Download Source Code

 

Other Projects to Try:

  1. Primitive operations on Sequential file in C language
  2. How to Implement Hash Table using C language
  3. Hash table code in C Language
  4. Matrix operations in c language
  5. Student Database in C Language

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

Matrix operations in c language

April 3, 2011 by ProjectsGeek Leave a Comment

Matrix operations in c language

 

Simple matrix operations in c language with Source Code for performing Matrix Operations in C and C++ Language . Operations on matrix which have to perform on matrix are as follows :

  • Input Operations
  • Display Operations
  • Addition Operations
  • Difference Operations
  • Multiplications Operations
  • Magic Square Operations
  • Saddle Point Operations
  • Transpose of Matrix

Above matrix operations in c language operations on matrix are divided into two categories as …..

  • Without Pointers
  • With Pointers

Matrix operations in c language Program Code

#include  
 #include  
 #define max 25  

 void input_p(int*,int*,int*);  
 void display_p(int*,int,int);  
 void add_p(int(*)[max],int(*)[max],int(*)[max],int,int);  
 void difference_p(int(*)[max],int(*)[max],int(*)[max],int,int);  
 void mul_p(int[][max],int[][max],int[][max],int,int,int);  
 int ms_p(int[][max],int);  
 int sp_p(int [][max],int,int);  
 void transpose_p(int a[][max],int m);  
 void input(int[][max],int*,int*);  
 void display(int[][max],int,int);  
 void add(int[][max],int[][max],int[][max],int,int);  
 void difference(int[][max],int[][max],int[][max],int,int);  
 void mul(int[][max],int[][max],int[][max],int,int,int);  
 int ms(int[][max],int);  
 int sp(int [][max],int,int);  
 void transpose(int a[][max],int m);  
 void withoutp(int [][max],int[][max],int*,int*,int*,int*);  
 void withp(int [][max],int[][max],int*,int*,int*,int*);  

 void main()  
 {  
 int a[max][max],b[max][max],c[max][max],r1,c1,r2,c2,ch;  
 r1=r2=c1=c2=0;  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\n\t\t>>>>----------  MAIN MENU ----------<<<<<\n\n");  
 printf("\n\t1\tWITH POINTERS\n");  
 printf("\n\t2\tWITHOUT POINTERS\n");  
 printf("\n\t3\tEXIT");  
 printf("\n\n\n\tENTER YOUR CHOICE\t");  
 scanf("%c",&ch);  
 switch(ch)  
 {  
 case '1':  
 withp(a,b,&r1,&c1,&r2,&c2);  
 break;  
 case '2':  
 withoutp(a,b,&r1,&c1,&r2,&c2);  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\t!!! INVALID CHOICE !!!");  
 getch();  
 }  
 }while(ch!='3');  
 getch();  
 }  //end of main  
 /******************************************************************  
 WITHP FUNCTION  
 ********************************************************************/  
 void withp(int a[][max],int b[][max],int* r1,int* c1,int* r2,int* c2)  
 {  
 int c[max][max],m;  
 char ch,ch1;  
 do  
 {  
 flushall();  
 clrscr();  
 printf("\n\n\t===>>>\tWITHOUT POINTERS MENU\t<<<===\n");  
 printf("\n\t1\tINPUT\n");  
 printf("\t2\tDISPLAY\n");  
 printf("\t3\tADDITION\n");  
 printf("\t4\tDIFFERENCE\n");  
 printf("\t5\tMULTIPLICATION\n");  
 printf("\t6\tMAGIC SQUARE\n");  
 printf("\t7\tSADDLE POINT\n");  
 printf("\t8\tTRANSPOSE\n");  
 printf("\t9\tRETURN TO MAIN MENU\n");  
 printf("\n\tENTER YOUR CHOICE\t:\t");  
 scanf("%c",&ch);  
 switch(ch)  
 {  
 case '1':  
 do  
 {  
 flushall();  
 clrscr();  
 printf("\n\t>>>>---- INPUT MENU ----<<<<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tRETURN\n");  
 printf("\tENTER YOUR CHOICE\t:\t");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 input_p(&a[0][0],r1,c1);  
 break;  
 case '2':  
 input_p(&b[0][0],r2,c2);  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\t!!! INVALID CHOICE !!!");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '2':  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\t>---- DISPLAY MENU ----<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tRETURN\n");  
 printf("\n\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r1==0 || *c1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 display_p(&a[0][0],*r1,*c1);  
 break;  
 case '2':  
 if(*r2==0 || *c2==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 display_p(&b[0][0],*r2,*c2);  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\n\t!!! INVALID CHOICE !!! ");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '3':  
 if(*r1==0 || *c1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 if(*r1==*r2 && *c1==*c2)  
 {  
 add_p(a,b,c,*r1,*c1);  
 printf("ADDITION HAS BEEN PERFORMED  >>>>>\n");  
 display_p(&c[0][0],*r1,*c1);  
 }  
 else  
 {  
 printf("\n\n\n\t!!! CAN'T ADD  !!!!\n");  
 }  
 getch();  
 break;  
 case '4':  
 if(*r1==*r2 && *c1==*c2)  
 {  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\t>>>>---- DIFFERENCE MENU -----<<<<\n");  
 printf("\n\t1\tMATRIX A - MATRIX B\n");  
 printf("\t2\tMATRIX B - MATRIX A\n");  
 printf("\t3\tRETURN\n");  
 printf("\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r1==0 || *c1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 difference_p(a,b,c,*r1,*c1);  
 printf("\n\n!!! SUBSTRACTION COMPLETE !!!\n");  
 display_p(&c[0][0],*r1,*c1);  
 break;  
 case '2':  
 if(*r2==0 || *c2==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 difference_p(b,a,c,*r1,*c1);  
 printf("\n\n!!! SUBSTRACTION COMPLETE !!!\n");  
 display_p(&c[0][0],*r1,*c1);  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\t!!! INVALID CHOICE !!!");  
 getch();  
 }  
 }while(ch1!='3');  
 }//end of if statement  
 else  
 printf("\n\n!!!  SUBSTRACTION NOT POSSIBLE !!!");  
 getch();  
 break;  
 case '5':  
 do  
 {  
 if(*r2==0 || *c2==0 ||*c1==0 ||*r1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 clrscr();  
 flushall();  
 printf("\t\t\t\tMULTIPLICATION MENU\n");  
 printf("\t1\tMATRIX A * MATRIX B\n");  
 printf("\t2\tMATRIX B * MATRIX A\n");  
 printf("\t3\tRETURN\n");  
 printf("\n\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r2==*c1)  
 {  
 mul_p(a,b,c,*r1,*r2,*c2);  
 printf("\n\t\t!!!  MULTIPLICATION COMPLETE !!!\n");  
 display_p(&c[0][0],*r1,*c2);  
 }  
 else  
 printf("\n\n\t!!! MULTIPLICATION NOT POSSIBLE !!!\n");  
 getch();  
 break;  
 case '2':  
 if(*r1==*c2)  
 {  
 mul_p(b,a,c,*r2,*r1,*c1);  
 printf("\n\t\t!!!  MULTIPLICATION COMPLETE !!!\n");  
 display_p(&c[0][0],*r2,*c1);  
 break;  
 }  
 else  
 printf("\n\n!!! MULTIPLICATION NOT POSSIBLE !!!\n");  
 getch();  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\t!!! INVALID CHOICE !!!");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '6':  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\t>>>>---- MAGIC SQUARE MENU ----<<<<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tRETURN\n");  
 printf("\tENTER YOUR CHOICE\t");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r1==0 || *c1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 if(*r1!=*c1)  
 printf("\n\n\t!!! NOT MAGIC SQUARE !!!\n");  
 else  
 {  
 if(ms_p(a,*r1)==0)  
 printf("\n\n\tTHE GIVEN MATRIX IS NOT A MAGIC SQUARE");  
 else  
 printf("\n\n\tTHE GIVEN MATRIX IS A MAGIC SQUARE\n");  
 }  
 getch();  
 break;  
 case '2':  
 if(*r2==0 || *c2==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 if(*r2 != *c2)  
 printf("\n\t!!! THE GIVEN MATRIX IS NOT A MGIC SQUARE !!!\n");  
 else  
 {  
 if(ms_p(b,*r2)==0)  
 printf("\n\t!!! THE GIVEN MATRIX IS NOT A MAGIC SQUARE  !!!");  
 else  
 printf("\n\t!!! THE GIVEN MATRIX IS A MAGIC SQUARE  !!!\n");  
 }  
 getch();  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\t!!! INVALID CHOICE !!!");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '7':  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\t>--- SADDLE POINT MENU ---<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tRERURN\n");  
 printf("\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r1==0 || *c1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 m=sp_p(a,*r1,*c1);  
 if(m==0)  
 printf("\n\n\t !!!! NO SADDLE POINTS !!!!");  
 else  
 printf("\n\n\tTOTAL %d SADDLE POINTS R PRESENT",m);  
 getch();  
 break;  
 case '2':  
 if(*r2==0 || *c2==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 m=sp_p(b,*r2,*c2);  
 if(m==0)  
 printf("\n\n\t !!!! NO SADDLE POINTS !!!!");  
 else  
 printf("\n\n\tTOTAL %d SADDLE POINTS R PRESENT",m);  
 getch();  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\n\t!!! INVALID CHOICE !!! ");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '8':  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\t>---- TRANSPOSE MENU ----<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tEXIT\n");  
 printf("\n\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r1==0 || *c1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 if(*r1>*c1)  
 transpose_p(a,*r1);  
 else  
 transpose_p(a,*c1);  
 m=*r1;  
 *r1=*c1;  
 *c1=m;  
 printf("\n\n\tMATRIX HAS BEEN TRANSPOSED\n");  
 display_p(&a[0][0],*r1,*c1);  
 break;  
 case '2':  
 if(*r2==0 || *c2==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 if(*r2>*c2)  
 transpose_p(b,*r2);  
 else  
 transpose_p(b,*c2);  
 m=*r2;  
 *r2=*c2;  
 *c2=*r2;  
 printf("\n\n\tMATRIX HAS BEEN TRANSPOSED\n");  
 display_p(&b[0][0],*r2,*c2);  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\n\t!!! INVALID CHOICE !!! ");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '9':  
 break;  
 default:  
  printf("\n\n\t!!! INVALID CHOICE !!!");  
  getch();  
 }  
 }while(ch!='9');  
 }  
 /***************************************************************  
 INPUT_P FUNCTION  
 ***************************************************************/  
 void input_p(int*a,int *r,int *c)  
 {  
 int i,j;  
 char st[max];  
 printf("NO. OF ROW \t");  
 scanf("%d",r);  
 printf("NO. OF COLUMN \t");  
 scanf("%d",c);  
 printf("\nENTER THE MATRIX \n");  
 for(i=0;i<*r;i++)  
 for(j=0;j<*c;j++)  
 {  
 scanf("%s",st);  
 if(valid(st)==1)  
 *(a+ i*(max) +j)=atoi(st);  
 else  
 {  
 printf("\n\t!!!  ENTER AGAIN !!!!");  
 j--;  
 }  
 }  
 }  
 /***************************************************************  
 DISPLAY_P FUNCTION  
 ***************************************************************/  
 void display_p(int*a,int r,int c)  
 {  
 int i,j;  
 printf("\n\nTHE MATRIX IS\n");  
 for(i=0;i<r;i++) <br=""> {  
 printf("\n\t");  
 for(j=0;j<c;j++) <br=""> {  
 printf("%-5d",*(a+ i*max +j) );  
 }  
 }  
 getch();  
 }  
 /***************************************************************  
 ADD_P FUNCTION  
 ***************************************************************/  
 void add_p(int (*a)[max],int (*b)[max],int(*c)[max],int r,int c1)  
 {  
 int i,j;  
 for(i=0;i<r;i++) <br=""> {  
 for(j=0;j<c1;j++) <br=""> {  
 *(c[i]+j) = *(a[i]+j) + *(b[i]+j);  
 }  
 }  
 }  
 /***************************************************************  
 DIFFERENCE_P FUNCTION  
 ***************************************************************/  
 void difference_p(int (*a)[max],int(*b)[max],int (*c)[max],int r,int c1)  
 {  
 int i,j;  
 for(i=0;i<r;i++) <br=""> {  
 for(j=0;j<c1;j++) <br=""> *(c[i]+j)= *(a[i]+j) - *(b[i]+j);  
 }  
 }  
 /***************************************************************  
 MUL_P FUNCTION  
 ***************************************************************/  
 void mul_p(int (*a)[max],int (*b)[max],int (*c)[max],int r1,int r2,int c2)  
 {  
 int i,j,k;  
 for(i=0;i<r1;++i) <br=""> {  
 for(j=0;j<c2;++j) <br=""> {  
 c[i][j]=0;  
 for(k=0;k<r2;++k) <br=""> *(c[i]+j) += *(a[i]+k) * ( *(b[k]+j) );  
 }  
 }  
 }  
 /***************************************************************  
 MS_P FUNCTION  
 ***************************************************************/  
 int ms_p(int (*a)[max],int r)  
 {  
 int s1=0,i,j,s2=0,s3=0;  
 for(i=0,j=r-1;i<r;i++,j--) <br=""> {  
 s1+=*(a[i]+i);  
 s2+=*(a[i]+j);  
 }  
 if(s1!=s2)  
 return 0;  
 for(i=0;i<r;i++) <br=""> {  
 s2=0;  
 s3=0;  
 for(j=0;j<r;j++) <br=""> {  
 s2+=*(a[i]+j);  
 s3+=*(a[j]+i);  
 }  
 if(s1!=s2 || s3!=s1)  
 return 0;  
 }  
 return 1;  
 }  
 /***************************************************************  
 SP_P FUNCTION  
 ***************************************************************/  
 int sp_p(int (*a)[max],int r,int c)  
 {  
 int i,j,k,m=0;  
 for(i=0;i<r;i++) <br=""> {  
 for(j=0;j<c;j++) <br=""> {  
 for(k=0;k<c;k++) <br=""> if( *(a[i]+j) > *(a[i]+k) )  
 break;  
 if(k==c)  
 {  
 for(k=0;k<r;k++) <br=""> if( *(a[i]+j) < *(a[k]+j) )  
 break;  
 if(k==r)  
 {  
 printf("\n\tSADDLE POINT AT (%d,%d) AND IS %d ",i+1,j+1,*(a[i]+j));  
 m++;  
 }  
 }  
 }//end of 2nd for  
 }  
 return m;  
 }  
 /***************************************************************  
 TRANSPOSE_P FUNCTION  
 ***************************************************************/  
 void transpose_p(int (*a)[max],int m)  
 {  
 int i,j,temp;  
 for(i=0;i<m;i++) <br=""> {  
 for(j=0;j<i;j++) <br=""> {  
 temp=*(a[i]+j);  
 *(a[i]+j)=*(a[j]+i);  
 *(a[j]+i)=temp;  
 }  
 }  
 }  
 /********************************************************************  
 WITOUTP FUNCTION  
 *********************************************************************/  
 void withoutp(int a[][max],int b[][max],int* r1,int* c1,int* r2,int* c2)  
 {  
 int c[max][max],m;  
 char ch,ch1;  
 do  
 {  
 flushall();  
 clrscr();  
 printf("\n\n\t===>>>\tWITHOUT POINTERS MENU\t<<<===\n");  
 printf("\n\t1\tINPUT\n");  
 printf("\t2\tDISPLAY\n");  
 printf("\t3\tADDITION\n");  
 printf("\t4\tDIFFERENCE\n");  
 printf("\t5\tMULTIPLICATION\n");  
 printf("\t6\tMAGIC SQUARE\n");  
 printf("\t7\tSADDLE POINT\n");  
 printf("\t8\tTRANSPOSE\n");  
 printf("\t9\tRETURN TO MAIN MENU\n");  
 printf("\n\tENTER YOUR CHOICE\t:\t");  
 scanf("%c",&ch);  
 switch(ch)  
 {  
 case '1':  
 do  
 {  
 flushall();  
 clrscr();  
 printf("\n\t>>>>---- INPUT MENU ----<<<<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tRETURN\n");  
 printf("\tENTER YOUR CHOICE\t:\t");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 input(a,r1,c1);  
 break;  
 case '2':  
 input(b,r2,c2);  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\t!!! INVALID CHOICE !!!");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '2':  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\t>---- DISPLAY MENU ----<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tRETURN\n");  
 printf("\n\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 display(a,*r1,*c1);  
 break;  
 case '2':  
 display(b,*r2,*c2);  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\n\t!!! INVALID CHOICE !!! ");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '3':  
 if(*r1==*r2 && *c1==*c2)  
 {  
 add(a,b,c,*r1,*c1);  
 printf("ADDITION HAS BEEN PERFORMED  >>>>>\n");  
 display(c,*r1,*c1);  
 }  
 else  
 {  
 printf("\n\n\n\t!!! CAN'T ADD  !!!!\n");  
 }  
 getch();  
 break;  
 case '4':  
 if(*r1==*r2 && *c1==*c2)  
 {  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\t>>>>---- DIFFERENCE MENU -----<<<<\n");  
 printf("\n\t1\tMATRIX A - MATRIX B\n");  
 printf("\t2\tMATRIX B - MATRIX A\n");  
 printf("\t3\tRETURN\n");  
 printf("\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 difference(a,b,c,*r1,*c1);  
 printf("\n\n!!! SUBSTRACTION COMPLETE !!!\n");  
 display(c,*r1,*c1);  
 break;  
 case '2':  
 difference(b,a,c,*r1,*c1);  
 printf("\n\n!!! SUBSTRACTION COMPLETE !!!\n");  
 display(c,*r1,*c1);  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\t!!! INVALID CHOICE !!!");  
 getch();  
 }  
 }while(ch1!='3');  
 }//end of if statement  
 else  
 printf("\n\n!!!  SUBSTRACTION NOT POSSIBLE !!!");  
 getch();  
 break;  
 case '5':  
 do  
 {  
 if(*r2==0 || *c2==0 ||*c1==0 ||r1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 clrscr();  
 flushall();  
 printf("\t\t\t\tMULTIPLICATION MENU\n");  
 printf("\t1\tMATRIX A * MATRIX B\n");  
 printf("\t2\tMATRIX B * MATRIX A\n");  
 printf("\t3\tRETURN\n");  
 printf("\n\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r2==*c1)  
 {  
 mul(a,b,c,*r1,*r2,*c2);  
 printf("\n\t\t!!!  MULTIPLICATION COMPLETE !!!\n");  
 display(c,*r1,*c2);  
 }  
 else  
 printf("\n\n\t!!! MULTIPLICATION NOT POSSIBLE !!!\n");  
 getch();  
 break;  
 case '2':  
 if(*r1==*c2)  
 {  
 mul(b,a,c,*r2,*r1,*c1);  
 printf("\n\t\t!!!  MULTIPLICATION COMPLETE !!!\n");  
 display(c,*r2,*c1);  
 break;  
 }  
 else  
 printf("\n\n!!! MULTIPLICATION NOT POSSIBLE !!!\n");  
 getch();  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\t!!! INVALID CHOICE !!!");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '6':  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\t>>>>---- MAGIC SQUARE MENU ----<<<<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tRETURN\n");  
 printf("\tENTER YOUR CHOICE\t");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r1==0 || *c1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 if(*r1!=*c1)  
 printf("\n\n\t!!! NOT MAGIC SQUARE !!!\n");  
 else  
 {  
 if(ms(a,*r1)==0)  
 printf("\n\n\tTHE GIVEN MATRIX IS NOT A MAGIC SQUARE");  
 else  
 printf("\n\n\tTHE GIVEN MATRIX IS A MAGIC SQUARE\n");  
 }  
 getch();  
 break;  
 case '2':  
 if(*r2==0 || *c2==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 if(*r2 != *c2)  
 printf("\n\t!!! THE GIVEN MATRIX IS NOT A MGIC SQUARE !!!\n");  
 else  
 {  
 if(ms(b,*r2)==0)  
 printf("\n\t!!! THE GIVEN MATRIX IS NOT A MAGIC SQUARE  !!!");  
 else  
 printf("\n\t!!! THE GIVEN MATRIX IS A MAGIC SQUARE  !!!\n");  
 }  
 getch();  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\t!!! INVALID CHOICE !!!");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '7':  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n\t>--- SADDLE POINT MENU ---<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tRERURN\n");  
 printf("\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r1==0 || *c1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 m=sp(a,*r1,*c1);  
 if(m==0)  
 printf("\n\n\t !!!! NO SADDLE POINTS !!!!");  
 else  
 printf("\n\n\tTOTAL %d SADDLE POINTS R PRESENT",m);  
 getch();  
 break;  
 case '2':  
 if(*r2==0 || *c2==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 m=sp(b,*r2,*c2);  
 if(m==0)  
 printf("\n\n\t !!!! NO SADDLE POINTS !!!!");  
 else  
 printf("\n\n\tTOTAL %d SADDLE POINTS R PRESENT",m);  
 getch();  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\n\t!!! INVALID CHOICE !!! ");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '8':  
 do  
 {  
 clrscr();  
 flushall();  
 printf("\n\n>---- TRANSPOSE MENU ----<\n");  
 printf("\t1\tMATRIX A\n");  
 printf("\t2\tMATRIX B\n");  
 printf("\t3\tEXIT\n");  
 printf("\n\tENTER YOUR CHOICE:");  
 scanf("%c",&ch1);  
 switch(ch1)  
 {  
 case '1':  
 if(*r1==0 || *c1==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 if(*r1>*c1)  
 transpose(a,*r1);  
 else  
 transpose(a,*c1);  
 m=*r1;  
 *r1=*c1;  
 *c1=m;  
 printf("\n\n\tMATRIX HAS BEEN TRANSPOSED\n");  
 display(a,*r1,*c1);  
 break;  
 case '2':  
 if(*r2==0 || *c2==0)  
 {  
 printf("\n\n\t!!! FIRST ENTER THE MATRIX !!!");  
 getch();  
 break;  
 }  
 if(*r2>*c2)  
 transpose(a,*r2);  
 else  
 transpose(a,*c2);  
 m=*r2;  
 *r2=*c2;  
 *c2=*r2;  
 printf("\n\n\tMATRIX HAS BEEN TRANSPOSED\n");  
 display(a,*r2,*c2);  
 break;  
 case '3':  
 break;  
 default:  
 printf("\n\n\n\t!!! INVALID CHOICE !!! ");  
 getch();  
 }  
 }while(ch1!='3');  
 break;  
 case '9':  
 break;  
 default:  
  printf("\n\n\t!!! INVALID CHOICE !!!");  
  getch();  
 }  
 }while(ch!='9');  
 }  
 /***************************************************************  
 INPUT FUNCTION  
 ***************************************************************/  
 void input(int a[][max],int *r,int *c)  
 {  
 int i,j;  
 char st[max];  
 printf("NO. OF ROW \t");  
 scanf("%d",r);  
 printf("NO. OF COLUMN \t");  
 scanf("%d",c);  
 printf("\nENTER THE MATRIX \n");  
 for(i=0;i<*r;i++)  
 for(j=0;j<*c;j++)  
 {  
 scanf("%s",st);  
 if(valid(st)==1)  
 a[i][j]=atoi(st);  
 else  
 {  
 printf("\n\t!!!  ENTER AGAIN !!!!");  
 j--;  
 }  
 }  
 }  
 /***************************************************************  
 VALID FUNCTION  
 ***************************************************************/  
 int valid(char st[])  
 {  
 int i=0;  
 for(;st[i]!=NULL;i++)  
 {  
 if(st[i]<48 ||st[i]>58)  
 return 0;  
 }  
 return 1;  
 }  
 /***************************************************************  
 DISPLAY FUNCTION  
 ***************************************************************/  
 void display(int a[][max],int r,int c)  
 {  
 int i,j;  
 printf("\n\nTHE MATRIX IS\n");  
 for(i=0;i<r;i++) <br=""> {  
 printf("\n\t");  
 for(j=0;j<c;j++) <br=""> {  
 printf("%-5d",a[i][j]);  
 }  
 }  
 getch();  
 }  
 /***************************************************************  
 ADD FUNCTION  
 ***************************************************************/  
 void add(int a[][max],int b[][max],int c[][max],int r,int c1)  
 {  
 int i,j;  
 for(i=0;i<r;i++) <br=""> {  
 for(j=0;j<c1;j++) <br=""> {  
 c[i][j]=a[i][j] + b[i][j];  
 }  
 }  
 }  
 /***************************************************************  
 DIFFERENCE FUNCTION  
 ***************************************************************/  
 void difference(int a[][max],int b[][max],int c[][max],int r,int c1)  
 {  
 int i,j;  
 for(i=0;i<r;i++) <br=""> {  
 for(j=0;j<c1;j++) <br=""> {  
 c[i][j]=a[i][j] - b[i][j];  
 }  
 }  
 }  
 /***************************************************************  
 MUL FUNCTION  
 ***************************************************************/  
 void mul(int a[][max],int b[][max],int c[][max],int r1,int r2,int c2)  
 {  
 int i,j,k;  
 for(i=0;i<r1;++i) <br=""> {  
 for(j=0;j<c2;++j) <br=""> {  
 c[i][j]=0;  
 for(k=0;k<r2;++k) <br=""> c[i][j]+=a[i][k] * b[k][j];  
 }  
 }  
 }  
 /***************************************************************  
 MS FUNCTION  
 ***************************************************************/  
 int ms(int a[][max],int r)  
 {  
 int s1=0,i,j,s2=0,s3=0;  
 for(i=0,j=r-1;i<r;i++,j--) <br=""> {  
 s1+=a[i][i];  
 s2+=a[i][j];  
 }  
 if(s1!=s2)  
 return 0;  
 for(i=0;i<r;i++) <br=""> {  
 s2=0;  
 s3=0;  
 for(j=0;j<r;j++) <br=""> {  
 s2+=a[i][j];  
 s3+=a[j][i];  
 }  
 if(s1!=s2 || s3!=s1)  
 return 0;  
 }  
 return 1;  
 }  
 /***************************************************************  
 SP FUNCTION  
 ***************************************************************/  
 int sp(int a[][max],int r,int c)  
 {  
 int i,j,k,m=0;  
 for(i=0;i<r;i++) <br=""> {  
 for(j=0;j<c;j++) <br=""> {  
 for(k=0;k<c;k++) <br=""> if(a[i][j] > a[i][k])  
 break;  
 if(k==c)  
 {  
 for(k=0;k<r;k++) <br=""> if(a[i][j] < a[k][j])  
 break;  
 if(k==r)  
 {  
 printf("\n\tSADDLE POINT AT (%d,%d) AND IS %d ",i+1,j+1,a[i][j]);  
 m++;  
 }  
 }  
 }//end of 2nd for  
 }  
 return m;  
 }  
 /***************************************************************  
 TRANSPOSE FUNCTION  
 ***************************************************************/  
 void transpose(int a[][max],int m)  
 {  
 int i,j,temp;  
 for(i=0;i<m;i++) <br=""> {  
 for(j=0;j<i;j++) <br=""> {  
 temp=a[i][j];  
 a[i][j]=a[j][i];  
 a[j][i]=temp;  
 }  
 }  
 }

 

Download Source Code

 

Other Projects to Try:

  1. Sparse Matrix Operations Code using C Langauge
  2. How to Implement Hash Table using C language
  3. Matrix Operations in C Language
  4. Matrix Operations in C Language
  5. Hash table code in C Language

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

String Operations in C Program

April 3, 2011 by ProjectsGeek Leave a Comment

String Operations in C Program

 

Problem Statement 

 

String Operations in C Program

Source Code to Implement String Operations in C Program or String Operation in C and C++ Language . Operations on String are as Follows :

  • Input Operations – This operation will ask for input from user.
  • Display Operations – This will display user the output.
  • Palindrome Operations – this function will check the entered string for palindrome.
  • Copy String – This option will make a copy of string.
  • Compare Operations on String – This option will compare the two entered strings according to requirement.
  • Reverse Strings – This function will reverse the string and then print to the screen.
  • Concatenate Operations on String – This function will concatenate two strings and then print the final string.
  • Finding Sub string Operations on String

Operations implementation by two methods

  • Using Pointers
  • Without Pointers

String Operations in C Program Code

 #include  
 #include  
 void withp(char []);  
 void withoutp(char []);  
 void in (char []);  
 void out(char []);  
 void palindrome(char []);  
 void reverse(char []);  
 void substring(char []);  
 void compare(char []);  
 void concat (char []);  
 int length (char []);  
 void copy(char []);  
 void input(char *);  
 void display(char *);  
 void palindrome1(char *);  
 void reverse1(char *);  
 void compare1(char *);  
 void concat1 (char *);  
 void substring1 (char *);  
 int length1 (char *);  
 void copy1(char *);  
 void main()  
 {  
 int ch;  
 char str[50];  
 str[0]=NULL;  
 clrscr();  
 do  
 {  
 clrscr();  
 printf("\n\n\n\t\t-------: MAIN MENU :--------");  
 printf("\n\n\n\t1\tUSING POINTERS \n ");  
 printf("\n\t2\tWITHOUT USING POINTERS\n");  
 printf("\n\t3\tEXIT\n\n");  
 printf("\n\tENTER UR CHOICE\t:   ");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1: withp(str);  
 break;  
 case 2:withoutp(str);  
 break;  
 case 3: break;  
 default:  
 printf("\n\n\t!!!!!!  INVALID CHOICE  !!!!!!!");  
 getch();  
 }  
 }while(ch!=3);  
 getch();  
 } //END OF MAIN  
 /*********************************************************************  
 WITH POINTERS FUNCTOIN  
 ************************************************************************/  
 void withp(char str[])  
 {  
 int ch,s1=0;  
 do  
 {  
 clrscr();  
 printf("\n\n\t\t------: USING POINTER MENU :------\n\n");  
 printf("\n\n\t1\tINPUT");  
 printf("\n\n\t2\tDISPLAY");  
 printf("\n\n\t3\tLENGHT OF THE STRING");  
 printf("\n\n\t4\tPALINDROME");  
 printf("\n\n\t5\tCOPY THE STRING");  
 printf("\n\n\t6\tCOMPARE THE STRING ");  
 printf("\n\n\t7\tREVERSE");  
 printf("\n\n\t8\tCONCATINATE THE STRING");  
 printf("\n\n\t9\tFIND THE SUBSTRING");  
 printf("\n\n\t10\tRETURN TO MAIN MENU");  
 printf("\n\n\n\tENTER UR CHOICE \t:  ");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 input(str);  
 break;  
 case 2:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 display(str);  
 getch();  
 break;  
 case 3:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 s1=length1(str);  
 printf("\n\n\tLENGTH OF THE STRING IS  %d",s1);  
 getch();  
 break;  
 case 4:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 palindrome1(str);  
 break;  
 case 5:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 copy(str);  
 break;  
 case 6:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 compare1(str);  
 break;  
 case 7:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 reverse1(str);  
 break;  
 case 8:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 concat1(str);  
 break;  
 case 9:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 substring1(str);  
 break;  
 case 10:  
 break;  
 default:  
 printf("\n\n\t!!!! INVALID CHOICE  !!!!!");  
 getch();  
 }  
 }while(ch!=10);  
 }  
 /*****************************************************************  
  INPUT FUNCTION  
 *******************************************************************/  
 void input(char *str)  
 {  
 int i=0;  
 printf("\n\n\tENTER THE STRING\t");  
 do  
 {  
 flushall();  
 *(str+i++)=getche();  
 } while(*(str+i-1)!='\r');  
 *(str+i)=NULL;  
 }  
 /*****************************************************************  
  DISPLAY FUNCTION  
 *******************************************************************/  
 void display(char *str)  
 {  
 int i=0;  
 printf("\n\n\t\t");  
 while(*(str+i)!='\r')  
 {  
 printf("%c",*(str+i++));  
 }  
 getch();  
 }  
 /*****************************************************************  
  PALINDROME FUNCTION  
 *******************************************************************/  
 void palindrome1(char *str)  
 {  
 int s,e,l=0;  
 l=length1(str);  
 for(s=0,e=l-1;s<e;s++,e--) <br=""> if(*(str+s)!=*(str+e))  
 break;  
 if(s==e)  
 printf("\n\n\t!!!!!!!! PALINDROME  !!!!!!!!");  
 else  
 printf("\n\n\t!!!!!!!! NOT PALINDROME  !!!!!!!!");  
   getch();  
 }  
 /*****************************************************************  
  REVERSE FUNCTION  
 *******************************************************************/  
 void reverse1(char *str)  
 {  
 int a,s,e,l=0;  
 l=length1(str);  
 for(s=0,e=l-1;s<e;s++,e--) <br=""> {  
 a=*(str+s);  
 *(str+s)=*(str+e);  
 *(str+e)=a;  
 }  
 printf("\n\n\t!!!!!!!!!!   STRING HAS BEEN REVERSED  !!!!!!!!!");  
 printf("\n\n\tREVERSED STRING  :-->>  ");  
 display(str);  
 }  
 /*****************************************************************  
  CONCATINATION  FUNCTION  
 *******************************************************************/  
 void concat1 (char *str)  
 {  
 int i=0,l1;  
 char *s1;  
 input(s1);  
 l1=length1(str);  
 while(*(s1+i)!='\r')  
  *(str+i+l1)=*(s1+i++);  
 *(str+i+l1)='\r';  
 *(str+i+l1+1)=NULL;  
 printf("\n\n\tCONCATINATED STRING :-->> ");  
 display(str);  
 }  
 /*****************************************************************  
  LENGTH FUNCTION  
 *******************************************************************/  
 int length1(char *str)  
 {  
 int i;  
 for(i=0;*(str+i)!=NULL;i++);  
 return --i;  
 }  
 /*****************************************************************  
  COMPARE FUNCTION  
 *******************************************************************/  
 void compare1(char *st)  
 {  
 int i,j,l1=0,l2=0;  
 char *s1;  
 input(s1);  
 l1=length1(st);  
 l2=length1(s1);  
 if(l1==l2)  
 {  
 for(i=0;i<l1;i++) <br="">  if(*(st+i)!=*(s1+i))  
  {  
 if(*(st+i) > *(s1+i))  
 printf("\n\n\tFIRST STRING IS GREATER THAN THE SECOND ");  
 else  
 printf("\n\n\tSECOND STRING IS GREATER THAN THE FIRST ");  
 break;  
  }  
 if(i==l2)  
 printf("\n\n\tBOTH THE STRINGS R EQUAL");  
 }  
 else  
 { if(l1>l2)  
 printf("\n\n\tFIRST STRING IS GREATER THAN THE SECOND ");  
 else  
 printf("\n\n\tSECOND STRING IS GREATER THAN THE FIRST ");  
 }  
 getch();  
 }  
 /*****************************************************************  
  COPY FUNCTION  
 *******************************************************************/  
 void copy1(char *st)  
 {  
 int i,j;  
 char *s1;  
 input(s1);  
 for(i=0;*(st+i)!=NULL;i++)  
  *(s1+i) = *(st+i);  
 *(s1+i)=NULL;  
 printf("\tTHE STRING IS COPIED");  
 display(s1);  
 getch();  
 }  
 /*****************************************************************  
  SUBSTRING FUNCTION  
 *******************************************************************/  
 void substring1(char *st)  
 { int j,i,m=0,l2=0;  
 char *s1;  
 input(s1);  
 l2=length1(s1);  
 for(i=0;*(st+i)!=NULL;i++)  
 if(*(st+i)==*(s1))  
 {  
 for(j=1;j<l2;j++) <br=""> if(*(st+i+j)!=*(s1+j))  
 break;  
 if(j==l2)  
 m++;  
 }  
 if(m>0)  
 printf("\n\n\t\tTHE STRING YOU HAVE ENTERED IS PRESENT %d TIMES",m);  
 else  
 printf("\nTHE STRING YOU HAVE ENTERED IS NOT A SUBSTRING OF THE STRING\n");  
 getch();  
 }  
 /*******************************************************  
 WITHOUT POINTERS  
 ********************************************************/  
 void withoutp(char str[])  
 {  
 int ch,s1=0;  
 clrscr();  
 do  
 {  
 clrscr();  
 printf("\n\n\t\t------: WITHOUT USING POINTER MENU :------\n\n");  
 printf("\n\n\t1\tINPUT");  
 printf("\n\n\t2\tDISPLAY");  
 printf("\n\n\t3\tLENGHT OF THE STRING");  
 printf("\n\n\t4\tPALINDROME");  
 printf("\n\n\t5\tCOPY THE STRING");  
 printf("\n\n\t6\tCOMPARE THE STRING ");  
 printf("\n\n\t7\tREVERSE");  
 printf("\n\n\t8\tCONCATINATE THE STRING");  
 printf("\n\n\t9\tFIND THE SUBSTRING");  
 printf("\n\n\t10\tRETURN TO MAIN MENU");  
 printf("\n\n\n\tENTER UR CHOICE \t:  ");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
  in(str);  
 break;  
 case 2:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\t!!!!!  ENTER THE STRING ");  
 getch();  
 break;  
 }  
 printf("\n\n\tTHE STRING IS \n\n\t\t ");  
 out(str);  
 break;  
 case 3:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\t!!!!!!!  ENTER THE STRING ");  
 getch();  
 break;  
 }  
 s1=length(str);  
 printf("\n\n\tLENGTH OF THE STRING IS  %d",s1);  
 getch();  
 break;  
 case 4:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\t!!!!!!!!!  ENTER THE STRING ");  
 getch();  
 break;  
 }  
 palindrome(str);  
 break;  
 case 5:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 copy(str);  
 break;  
 case 6:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 compare(str);  
 break;  
 case 7:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 reverse(str);  
 break;  
 case 8:  
 if(s1==0)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 concat(str);  
 break;  
 case 9:  
 if(str[0]==NULL)  
 {  
 printf("\n\n\tENTER THE STRING ");  
 getch();  
 break;  
 }  
 substring(str);  
 break;  
 case 10:  
 break;  
 default:  
 printf("\n\n\t!!!! INVALID CHOICE  !!!!!");  
 getch();  
 }  
 }while(ch!=10);  
 }  
 /*****************************************************************  
  INPUT FUNCTION  
 *******************************************************************/  
 void in(char st[])  
 {  
 int i=0;  
 printf("\n\n\tENTER THE STRING\t");  
 do  
 {  
 flushall();  
 st[i++]=getche();  
 } while(st[i-1]!='\r');  
 st[i]=NULL;  
 }  
 /*****************************************************************  
  OUTPUT FUNCTION  
 *******************************************************************/  
 void out(char st[])  
 {  
 int i=0;  
 printf("\n\n\t\t");  
 while(st[i]!=NULL)  
 {  
 printf("%c",st[i++]);  
 }  
 getch();  
 }  
 /*****************************************************************  
  PALINDROME FUNCTION  
 *******************************************************************/  
 void palindrome (char st[])  
 {  
 int s,e,l=0;  
 l=length(st);  
 for(s=0,e=l-1;s<e;s++,e--) <br=""> if(st[s]!=st[e])  
 break;  
 if(s==e)  
 printf("\n\n\t!!!!!!!! PALINDROME  !!!!!!!!");  
 else  
 printf("\n\n\t!!!!!!!! NOT PALINDROME  !!!!!!!!");  
   getch();  
 }  
 /*****************************************************************  
  REVERSE FUNCTION  
 *******************************************************************/  
 void reverse (char st[])  
 {  
 int a,s,e,l=0;  
 l=length(st);  
 for(s=0,e=l-1;s<e;s++,e--) <br=""> {  
 a=st[s];  
 st[s]=st[e];  
 st[e]=a;  
 }  
 printf("\n\n\t!!!!!!!!!!   STRING HAS BEEN REVERSED  !!!!!!!!!");  
 printf("\n\n\tREVERSED STRING  :-->>  ");  
 out(st);  
 }  
 /*****************************************************************  
  COPY FUNCTION  
 *******************************************************************/  
 void copy(char st[])  
 {  
 int i,j,l=0;  
 char s1[50];  
 l=length(st);  
 for(i=0;i<l;i++) <br=""> s1[i]=st[i];  
 s1[i]=NULL;  
 printf("\tTHE STRING IS COPIED");  
 out(s1);  
 getch();  
 }  
 /*****************************************************************  
  COMPARE FUNCTION  
 *******************************************************************/  
 void compare(char st[])  
 {  
 int i,j,l1=0,l2=0;  
 char s1[50];  
 in(s1);  
 l1=length(st);  
 l2=length(s1);  
 if(l1==l2)  
 {  
 for(i=0;i<l2;i++) <br="">  if(st[i]!=s1[i])  
  {  
 if(st[i]>s1[i])  
 printf("\n\n\tFIRST STRING IS GREATER THAN THE SECOND ");  
 else  
 printf("\n\n\tSECOND STRING IS GREATER THAN THE FIRST ");  
 break;  
  }  
 if(i==l2)  
 printf("\n\n\tBOTH THE STRINGS R EQUAL");  
 }  
 else  
 { if(l1>l2)  
 printf("\n\n\tFIRST STRING IS GREATER THAN THE SECOND ");  
 else  
 printf("\n\n\tSECOND STRING IS GREATER THAN THE FIRST ");  
 }  
 getch();  
 }  
 /*****************************************************************  
  SUBSTRING FUNCTION  
 *******************************************************************/  
 void substring (char st[])  
 {  
 int i,j,m=0,l1=0,l2=0;  
 char s1[50];  
 in(s1);  
 l1=length(st);  
 l2=length(s1);  
 for(i=0;i<l1;i++) <br=""> {  
 if(st[i]==s1[0])  
 {  
 for(j=0;j<l2;j++) <br=""> if(st[i+j]!=s1[j])  
 break;  
 if(j==l2)  
 m++;  
 }  
 }  
 if(m!=0)  
 printf("\n\n\t SUBSTRING IS PRESENT %d TIMES IN THE STRING",m);  
 else  
 printf("\n\n\tTHE SUBSTRING IS NOT PRESENT");  
 getch();  
 }  
 /*****************************************************************  
  CONCATINATION FUNCTION  
 *******************************************************************/  
 void concat (char st[])  
 {  
 int i=0,l1=0;  
 char s1[50];  
 l1=length(st);  
 while(s1[i]!='\r')  
 st[i+l1]=s1[i++];  
 st[i+l1]=NULL;  
 printf("\n\n\tCONCATINATED STRING :-->> ");  
 out(st);  
 }  
 /*****************************************************************  
  LENGTH FUNCTION  
 *******************************************************************/  
 int length (char st[])  
 {  
 int i;  
 for(i=0;st[i]!=NULL;i++);  
 return --i;  
 }  
 /*****************************************************************  
  THANKS  
 *******************************************************************/

Download Source Code

 

Other Projects to Try:

  1. string operations such as Copy, Length, Reversing, Palindrome, Concatenation
  2. String Operations with Pointers
  3. String Operations in C Language
  4. Matrix operations in c language
  5. Set Operations program in C

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

  • « Go to Previous Page
  • Page 1
  • Interim pages omitted …
  • Page 132
  • Page 133
  • Page 134
  • Page 135
  • Go to Next Page »

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy