• 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

Fundamentals of Datastructure

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

Set Operations program in C

April 3, 2011 by ProjectsGeek 2 Comments

Set Operations program in C

 

 

Set Operations program in CPrograms source code to perform Set Operations program in C and C++ Language. Operations on set to be performed are Given below

  • Input Operations – This operation should allow the user to provide input to the program. The program should check the provided input to see whether it’s valid or not if the input is not valid then the program should ask the user to provide input again.
  • Display Operations – This operation should present the user with the requested output. Display operations will have another sub-menu to select other options.
  • Union Operation – This operation of the program will implement the union operation on the provided input and then display the final output.
  • Intersection Operations – This option should implement intersection operation on the provided input and output the result. 
  • Difference Operations – This operation should implement a difference operation on the provided sets. After calculating the difference user should be provided with the output.
  • Symmetric Difference Operations – This option of the Set Operations program in C should implement symmetric difference operations to the sets.
  • Exit – Upon selecting this option, the program should quit.

These operations on sets are performed by implementing a simple logic in C language code. Users have to provide input to the Operations on Set program and it will print the selected operation on Set.

Set Operations Program Code 

 

#include  
 #include  
 int _union(int[],int[],int[],int,int);  
 int input(int[]);  
 int intersection(int[],int[],int[],int,int);  
 int difference(int[],int[],int[],int,int);  
 int symdiff(int[],int[],int[],int,int);  
 void display(int[],int);  
 void main()  
 {  
 int set1[20],set2[20],set3[40],s1,s2,s3,ch,ch1,ch2;  
 clrscr();  
 do  
 {  
 clrscr();  
 printf("\n\n\n\t\t\t------: MENU :--------\n\n");  
 printf("\n\t1.\tINPUT\n");  
 printf("\n\t2.\tDISPLAY\n");  
 printf("\n\t3.\tUNION \n");  
 printf("\n\t4.\tINTERSECTION\n");  
 printf("\n\t5.\tDIFFERENCE\n");  
 printf("\n\t6.\tSYMMETRIC DIFFERENCE\n");  
 printf("\n\t7.\tEXIT\n");  
 printf("\n\n\n\t\tPLEASE ENTER UR CHOICE\t:- ");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1 :  
 do{  
 clrscr();  
 printf("\n\n\n\t\t\t-------: INPUT MENU :-------\n\n");  
 printf("\n\t1.\tSET1\n");  
 printf("\n\t2.\tSET2\n");  
 printf("\n\t3.\tRETURN TO MAIN MENU\n");  
 printf("\n\t\tENTER UR CHOICE  :-  ");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 s1=input(set1);  
 break;  
 case 2:  
 s2=input(set2);  
 break;  
 case 3:  
 break;  
 default:  
 printf("\n\n\tINVALID CHOICE");  
 getch();  
 }  
 }while(ch1!=3);  
 break;  
 case 2:  
 do  
 {  
 clrscr();  
 printf("\n\n\n\t\t\t-------: OUTPUT MENU :-------\n\n");  
 printf("\n\t1.\tSET1\n");  
 printf("\n\t2.\tSET2\n");  
 printf("\n\t3.\tRETURN TO MAIN MENU\n");  
 printf("\n\t\tENTER UR CHOICE  :-  ");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 display(set1,s1);  
 break;  
 case 2:  
 display(set2,s2);  
 break;  
 case 3:  
 break;  
 default:  
 printf("\n\n\tINVALID CHOICE");  
 getch();  
 }  
 }while(ch1!=3);  
 break;  
 case 3 :  
 if(s1==0 &&s2==0)  
 printf("\n\n\t\tU HAVE TO ENTER SET FIRST");  
 else  
 {  
 s3=_union(set1,set2,set3,s1,s2);  
 display(set3,s3);  
 }  
 break;  
 case 4 :  
 if(s1==0 &&s2==0)  
 printf("\n\n\t\tU HAVE TO ENTER SET FIRST");  
 else  
 {  
 s3=intersection(set1,set2,set3,s1,s2);  
 display(set3,s3);  
 }  
 break;  
 case 5 :  
 if(s1==0 &&s2==0)  
 {  
  printf("\n\n\t\tU HAVE TO ENTER SET FIRST");  
  break;  
 }  
 do  
 {  
 clrscr();  
 printf("\n\n\n\t\t\t-------: DIFFERENCE MENU :-------\n\n");  
 printf("\n\t1.\tSET1 - SET2\n");  
 printf("\n\t2.\tSET2 - SET1\n");  
 printf("\n\t3.\tRETURN TO MAIN MENU\n");  
 printf("\n\t\tENTER UR CHOICE  :-  ");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1: s3=difference(set1,set2,set3,s1,s2);  
 display(set3,s3);  
 break;  
 case 2: s3=difference(set2,set1,set3,s2,s1);  
 display(set3,s3);  
 break;  
 case 3:  
 break;  
 default:  
 printf("\n\n\tINVALID CHOICE");  
 getch();  
 }  
 }while(ch1!=3);  
 break;  
 case 6:  
 if(s1==0 &&s2==0)  
 {  
  printf("\n\n\t\tU HAVE TO ENTER SET FIRST");  
  break;  
 }  
 s3=symdiff(set1,set2,set3,s1,s2);  
 display(set3,s3);  
 break;  
 case 7: break;  
 default:  
 printf("\n\n\tINVALID CHOICE");  
 getch();  
 }  
 }while(ch!=7);  
 getch();  
 }   //END OF MAIN  
 /*****************************************************  
 INPUT FUNCTION  
 *****************************************************/  
 int input(int set[])  
 {  
 int i=0,j,s;  
 printf("\n\tENTER THE SIZE OFTHE SET\t");  
 scanf("%d",&s);  
 printf("\n\tENTER THE SET\t\t\n");  
 while(i<s) <br=""> {  
 scanf("%d",&set[i]);  
 for(j=0;j<i;j++) <br=""> if(set[i]==set[j])  
 {  
 printf("\n\n\t!!!! DUPLICATED ENTRY !!!!\n\n");  
 printf("\t\t----------: ENTER AGAIN");  
 break;  
 }  
 if(i==j)  
 i++;  
 }  
 return s;  
 }  
 /*********************************************************  
 UNION FUNCTION  
 *********************************************************/  
 int _union(int set1[],int set2[],int set3[],int s1,int s2)  
 {  
 int s3,i,j;  
 for(i=0;i<s1;i++) <br=""> set3[i]=set1[i];  
 s3=i;  
 for(i=0;i<s2;i++) <br=""> {  
 for(j=0;j<s1;j++) <br=""> if(set1[j]==set2[i])  
 break;  
 if(j==s1)  
 set3[s3++]=set2[i];  
 }  
 return s3;  
 }  
 /*************************************************************  
  INTERSECTION FUNCTION  
 ***************************************************************/  
 int intersection(int set1[],int set2[],int set3[],int s1,int s2)  
 {  
 int i,j,s3=0;  
 for(i=0;i<s1;i++) <br=""> {  
 for(j=0;j<s2;j++) <br=""> if(set1[i]==set2[j])  
 {  
 set3[s3++]=set1[i];  
 break;  
 }  
 }  
 return s3;  
 }  
 /****************************************************************  
 DIFFERENCE FUNCTION  
 ******************************************************************/  
 int difference(int set1[],int set2[],int set3[],int s1,int s2)  
 {  
 int i,j,s3=0;  
 for(i=0;i<s1;i++) <br=""> {  
 for(j=0;j<s2;j++) <br=""> if(set1[i]==set2[j])  
 break;  
 if(j==s2)  
 set3[s3++]=set1[i];  
 }  
 return s3;  
 }  
 /**********************************************************  
  SYMDIFF FUNCTION  
 ***********************************************************/  
 int symdiff(int set1[],int set2[],int set3[],int s1,int s2)  
 {  
 int s3=0,se1[20],se2[20],si1,si2;  
 si1=difference(set1,set2,se1,s1,s2);  
 si2=difference(set2,set1,se2,s2,s1);  
 s3=_union(se1,se2,set3,si1,si2);  
 return s3;  
 }  
 /*********************************************************  
  DISPLAY FUNCTION  
 **********************************************************/  
 void display(int set[],int size)  
 {  
 int i;  
 printf("\n\n\t{ ");  
 for(i=0;i<size;i++) <br=""> printf("\t%d",set[i]);  
 printf("\t} ");  
 getch();  
 }
Download Code Here

Other Projects to Try:

  1. Set operations – Union, Intersection, Difference, Symmetric Difference using C
  2. Matrix operations in c language
  3. String Operations in C Program
  4. Sparse Matrix Operations Code using C Langauge
  5. Matrix Operations in C Language

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3

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