Skip to main content

Structure within Function in C

#include<stdio.h>
 #include<conio.h>
   struct stores
    {
      char name[20];
      float price;
      int quantity;
      };
      struct stores update(struct stores product, float p, int q)
      {
	product.price+=p;
	product.quantity+=q;
	return(product);
      }
      float mul(struct stores stock)
      {
       return(stock.price*stock.quantity);
      }
    void main(void)
     {
       clrscr();
       float p_inc,value;
       int q_inc;
       static struct stores item={"XYZ",25.75,12};
       printf("\n Input inc values:\n");
       printf("\n Price inc  and Quantity inc\n\n");
       scanf("%f %d",&p_inc,&q_inc);
       item=update(item,p_inc,q_inc);
       printf("Update values of item\n\n");
       printf("name     :%s\n",item.name);
       printf("Price     :%f\n",item.price);
       printf("Quantity   :%d\n",item.quantity);
       value=mul(item);
       printf("\n values of the item =%f\n",value);
       getch();
     }

Pattern Matching in C

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
	{
	int i,j,k=-1,s,r,max,comp=0;
	char t[50],p[50],c;
	clrscr();
	printf("ENTER TEXT = ");gets(t);s=strlen(t);
	PATTERN:
	printf("ENTER PATTERN = ");gets(p);r=strlen(p);
	max=s-r+1;
	//SEARCHING FOR PATTERN//
	for(i=0;i<=max;i++)
		{
		for(j=0;j<r;j++)
			{
			k=i;
			if(p[j]==t[i+j])
				{
				comp++;
				if(comp==r)
					{
					goto LOCK;
					}
				}
			else
				{
				k=-1;
				comp=0;
				break;
				}
			}
		}
	LOCK:
	if(k>-1)
		{
		printf("\nPATTERN IS AT LOCATION %d",k+1);
		}
	else
		{
		printf("\n\aSORRY PATTERN CANN'T FIND.");
		}
	printf("\nFIND PATTERN AGAIN ? (Y/N)..");
	c=getch();
	if(c=='y'||c=='Y')
		{
		k=-1;
		comp=0;
		goto PATTERN;
		}
	else
		exit(0);
	}


Multi Delete in C

#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include <stdlib.h>

void main()
	{
	int i,s,index1,index2,loc,data[50];
	char c;
	clrscr();
	printf("HOW MANY ELEMENT YOU WANT FOR ARRAY:\t");scanf("%d",&index1);
	clrscr();
	printf("START ENTERING VALUE:\n");
	for(i=1;i<=index1;i++)
		{
		printf("data[%d]=",i);
		scanf("%d",&data[i]);
		}
	MULTI_DELET:
	printf("\nENTER LOCATION TO DELET:");scanf("%d",&loc);
	if(loc>index1)
		{
		printf("\n\aLOCATION IS OUT OF RANGE.TRY AGAIN...");
		goto MULTI_DELET;
		}
	printf("HOW MANY INDEX YOU WANT TO DELET:\t");scanf("%d",&index2);
	if(index2>index1)
		{
		printf("\n\aOUT OF RANGE.TRY AGAIN...");
		goto MULTI_DELET;
		}

		//SHIFTING//
	s=index1-(loc+index2);
	for(i=0;i<=s;i++)
		{
		data[loc+i]=data[i+loc+index2];
		}
		//DISPLAY//
	 printf("\nAFTER DELETING");
	 for(i=1;i<=index1-index2;i++)
		{
		printf("\ndata[%d]=%d",i,data[i]);
		}
	 index1=index1-index2;
	 if(index1<=0)
		{
		printf("\n\aSORRY.THERE IS NO MORE ELEMENT TO DISPALY..");
		printf("\n\n\t******* GOOD BYE ********");
		delay(3000);
		exit(1);
		}

	 printf("\nDO IT AGAIN ?(y/n)...");
	 c=getch();
	 if(c=='y'||c=='Y')
		{
		goto MULTI_DELET;
		}
	}

Linked List with Student Information & Count the Number of Node in C

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define NULL 0

struct linked_list
		{
		int roll;
		char name[20];
		struct linked_list *next;
		};
typedef struct linked_list node;

void create(node *list)
	{
	printf("Enter the ROLL NUMBER:\t");
	scanf("%d",&list->roll);
	if(list->roll<0)
		{
		list->next=NULL;
		}
	else
		{
		printf("Enter the NAME:\t");
		scanf("%c",list->name);
		list->next=(node *)malloc(sizeof(node));
		create(list->next);
		}
	return;
	}

void print(node *list)
	{
	if(list->next!=NULL)
		{
		printf("ROLL NO: %d\n",list->roll);
		printf("ROLL NO: %f\n",list->name);
		print(list->next);
		}
	return;
	}

int count(node *list)
	{
	if(list->next==NULL)
		return(0);
	else
		return(1+count(list->next));
	}
void main(void)
	{
	clrscr();
	node *head;
	head=(node *)malloc(sizeof(node));
	printf("\nTo QUIT, Enter any NEGATIVE NUMBER..\n");
	create(head);
	printf("\n\n");
	printf("\n\tPRINTING THR TOTAL LINK LIST.....");
	print(head);
	printf("THE NUMBER OF NODE IS %d",count(head));
	getch();
	}

Pattern Matching by Function in C

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

void SEARCHING_PATTERN(char t[50],char p[50])
	{
	int s,r,max,i,j,k=-1,comp=0;
	s=strlen(t);
	r=strlen(p);
	max=s-r+1;
	//SEARCHING FOR PATTERN//
	for(i=0;i<=max;i++)
		{
		for(j=0;j<r;j++)
			{
			k=i;
			if(p[j]==t[i+j])
				{
				comp++;
				if(comp==r)
					{
					goto LOCK;
					}
				}
			else
				{
				k=-1;
				comp=0;
				break;
				}
			}
		}
	LOCK:
	if(k>-1)
		{
		printf("\nPATTERN IS AT LOCATION %d",k+1);
		}
	else
		{
		printf("\n\aSORRY PATTERN CANN'T FIND.");
		}
	}

void main()
	{
	int k=-1,comp=0;
	char t[50],p[50],c;
	clrscr();
	printf("ENTER TEXT = ");gets(t);
	PATTERN:
	printf("ENTER PATTERN = ");gets(p);
	SEARCHING_PATTERN(t,p);
	printf("\nFIND PATTERN AGAIN ? (Y/N)..");
	c=getch();
	if(c=='y'||c=='Y')
		{
		k=-1;
		comp=0;
		goto PATTERN;
		}
	else
		exit(0);
	}

Multi-Insert in C

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
	{
	clrscr();
	int i,j,n1,n2,l,v,t,data[50],a[30];
	char c;
	printf("how many element u want:\t");	scanf("%d",&n1);
	for(i=1;i<=n1;i++)
		{
		printf("data[%d]= ",i);	 scanf("%d",&data[i]);
		}
	again:
	printf("\nEnter location to insert:\t");  scanf("%d",&l);
	printf("\nHow many element u want:\t");   scanf("%d",&n2);
	for(i=1;i<=n2;i++)
		{
		printf("a[%d]= ",i);  scanf("%d",&a[i]);
		}
		//shifting//
	for(i=(n1+n2),j=0;i>=n2+l;i--,j++)
		{
		data[i]=data[n1-j];
		}
		//entering value//
	for(i=0;i<n2;i++)
		{
		data[l+i]=a[i+1];
		}
		//display//
	for(i=1;i<=n1+n2;i++)
		{
		printf("data[%d]=%d\n",i,data[i]);
		}
	printf("\ninsert again ? (y/n)..");
	c=getch();
	if(c=='y')
		{
		n1=n1+n2;
		goto again;
		}
	else
		exit(0);
	}

Quick Sort in C

#include<stdio.h>
#include<conio.h>

int comp=0,s=0;
int quick(int array[],int n,int beg,int end,int loc)
	{
	int left=beg,right=end,temp;
	loc=beg;
	right_to_left:
	while(array[loc]<=array[right] && loc!=right)
		{
		right=right-1;
		comp++;
		}
	if(loc==right)
		return(loc);
	if(array[loc]>array[right])
		{
		s++;
		temp=array[loc];
		array[loc]=array[right];
		array[right]=temp;
		loc=right;
		goto left_to_right;
		}
	left_to_right:
	while(array[left]<=array[loc] && left!=loc)
		{
		left=left+1;
		comp++;
		}
	if(loc==left)
		return (loc);
	if(array[left]>array[loc])
		{
		s++;
		temp=array[loc];
		array[loc]=array[left];
		array[left]=temp;
		loc=left;
		goto right_to_left;
		}
	}
void main()
	{
	int array[30],n,i,top=0,lower[10],upper[10];
	int beg,end,loc;
	printf("HOW MANY ELEMENTYOU WANT: ");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		{
		printf("array[%d]",i);
		scanf("%d",&array[i]);
		}
	if(n>1)
		{
		top=top+1;
		lower[1]=1;
		upper[1]=n;
		}
	while(top!=0)
		{
		beg=lower[top];
		end=upper[top];
		top=top-1;
		loc=quick(array,n,beg,end,loc);
		if(beg<loc-1)
			{
			top=top+1;
			lower[top]=beg;
			upper[top]=loc-1;
			}
		if(loc+1<end)
			{
			top=top+1;
			lower[top]=loc+1;
			upper[top]=end;
			}
		}
	printf("\nAFTER SORTING");
	for(i=1;i<=n;i++)
		{
		printf("\nARRAY[%d]=%d",i,array[i]);
		}
	getch();
	}


Replace String from another String in C

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>

int INDEX(char text[],char string[],int tlength,int slength)
	{
	int i,j,max,position=-1;
	max=tlength-slength+1;
	for(i=1;i<=max;i++)
		{
		if(position!=-1)
			{
			break;
			}
		for(j=0;j<slength;j++)
			{
			if(string[j]==text[i+j-1])
				{
				if(position==-1)
					{
					position=i;
					}
				}
			else
				{
				position=-1;
				break;
				}
			}
		}
	return (position);
	}

int DEL(char text[],int position,int s1length)
	{
	int tlength,i,j;
	tlength=strlen(text);
	for(i=position+s1length-1,j=position-1;i<tlength;i++,j++)
		{
		text[j]=text[i];
		}
	return (tlength-s1length);
	}

void INSERT(char text[],int position,int ntlength,char string2[])
	{
	int i,j,s2length;
	s2length=strlen(string2);
	for(i=ntlength-1;i>=position-1;i--)
		{
		text[i+s2length]=text[i];
		}
	for(i=0,j=position-1;i<s2length;i++,j++)
		{
		text[j]=string2[i];
		}
	for(i=0;i<ntlength+strlen(string2);i++)
		{
		printf("%c",text[i]);
		}
	}

void main()
	{
	clrscr();
	char text[30],string1[30],string2[30];
	int tlength,s1length,s2length,ntlength,position;

	printf("ENTER ORIGINAL TEXT:\t");
	gets(text);
	tlength=strlen(text);
	AGAIN:
	printf("\nENTER STRING TO BE REPLACE:\t");
	gets(string1);
	s1length=strlen(string1);
	printf("\nENTER STRING TO INSERTED:\t");
	gets(string2);
	s2length=strlen(string2);
	if(s1length>tlength)
		{
		printf("\n\aTHE TARGET STRING IS GERATER THAN ORIGINAL STRING");
		goto AGAIN;
		}
	position=INDEX(text,string1,tlength,s1length);
	if(position==-1)
		{
		printf("\n\aTHE TARGET IN NOT FOUND..! ");
		}
	else
		{
		ntlength=DEL(text,position,s1length);
		INSERT(text,position,ntlength,string2);
		}
	getch();
	}

Bubble Sort in C

#include<stdio.h>
#include<conio.h>

void main()
	{
	clrscr();
	int i,j,n,t,DATA[50];
	printf("HOW MANY ELEMENT YOU WANT:\t");
	scanf("%d",&n);
	printf("START ENTERING VALUE..\n");
	for(i=1;i<=n;i++)
		{
		printf("DATA[%d]= ",i);
		scanf("%d",&DATA[i]);
		}
		/*start sorting BY ASSENDING ORDER..*/
	printf("\nDATA SORTED BY ASSENDING ORDER..");
	for(i=1;i<=n;i++)
		{
		for(j=i+1;j<=n;j++)
			{
			if(DATA[i]>DATA[j])
				{
				t=DATA[i];
				DATA[i]=DATA[j];
				DATA[j]=t;
				}
			}
		printf("\nDATA[%d]=%d",i,DATA[i]);
		}
		/*start sorting BY DISSENDING ORDER..*/
	printf("\nDATA SORTED BY DISSENDING ORDER..");
	for(i=1;i<=n;i++)
		{
		for(j=i+1;j<=n;j++)
			{
			if(DATA[i]<DATA[j])
				{
				t=DATA[i];
				DATA[i]=DATA[j];
				DATA[j]=t;
				}
			}
		printf("\nDATA[%d]=%d",i,DATA[i]);
		}
	getch();
	}

Binary Search Using Function in C

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void sorting(int DATA[50],int x);
void find_location(int DATA[50],int x);
void read_value(int DATA[50],int x)
	{
	int i;
		//READING VALUE//
	for(i=1;i<=x;i++)
		{
		printf("DATA[%d]=",i);	scanf("%d",&DATA[i]);
		}
	sorting(DATA,x);
	}
void sorting(int DATA[50],int x)
	{
	int i,j,t;
		//START SORTING//
	for(i=1;i<=x-1;i++)
		{
		for(j=i+1;j<=x;j++)
			{
			if(DATA[i]>DATA[j])
				{
				t=DATA[i];
				DATA[i]=DATA[j];
				DATA[j]=t;
				}
			}
		}
	printf("AFTER SORTING..\n");
	for(i=1;i<=x;i++)
		{
		printf("\nDATA[%d]=%d",i,DATA[i]);
		}
	find_location(DATA,x);
	}
void find_location(int DATA[50],int x)
	{
	int v,beg,end,mid;
	printf("\nENTER VALUE TO FIND:\t"); scanf("%d",&v);
	beg=1;	end=x; 	mid=(beg+end)/2;
	while(DATA[mid]!=v && beg<=end)
		{
		if(v>DATA[mid])
			{
			beg=mid+1;
			}
		else
			{
			end=mid-1;
			}
		mid=(beg+end)/2;
		}
	if(DATA[mid]==v)
		{
		printf("\n%d IS AT LOCATION %d",v,mid);
		}
	else
		{
		printf("\n\aLOCATION IS NOT FIND");
		}
	}
void main()
	{
	clrscr();
	int i,t,j,n,beg,end,mid,DATA[50],loc,v;
	char c;
	printf("HOW MANY ELEMENT YOU WANT:\t");  scanf("%d",&n);
	read_value(DATA,n);
	BINARY_SEARCH:
	find_location(DATA,n);
	printf("\nTRY AGAIN ? (Y/N)..");
	c=getch();
	if(c=='y'||c=='Y')
		{
		goto BINARY_SEARCH;
		}
	else
		exit(0);
	}