Skip to main content

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);
	}

Normal Insert by Using Function in C

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

	void TAKING_VALUE(int DATA[50],int x)
		{
		int i;
		for(i=1;i<=x;i++)
			{
			printf("DATA[%d]=",i);
			scanf("%d",&DATA[i]);
			}
		}

	void INSERT(int DATA[50],int x)
		{
		int loc,v,i,k,t;
		INSERT:
		printf("\nENTER LOCATION TO INSERT:\t");
		scanf("%d",&loc);
		if(loc>x)
		      {
		      printf("\n\aSORRY OOCATION IS OUT OF RANGE. TRY AGAIN..");
		      goto INSERT;
		      }
		printf("\nENTER VALUE:\t");
		scanf("%d",&v);
		printf("\nAFTER INSERT..");
		for(i=1;i<=x+1;i++)
			{
			if(loc==i)
				{
				for(k=i;k<=x+1;k++)
					{
					t=DATA[k];
					DATA[k]=v;
					v=t;
					}
				}
			printf("\nDATA[%d]=%d",i,DATA[i]);
			}
		}

	void main()
		{
		int i,n,k,t,DATA[50],loc,v;
		char c;
		clrscr();
		printf("HOW MANY ELEMENT YOU WANT:\t");
		scanf("%d",&n);
		TAKING_VALUE(DATA,n);
		AGAIN:
		INSERT(DATA,n);
		printf("\nINSERT AGAIN ? (y/n)..");
		c=getch();
		if(c=='y'||c=='Y')
			{
			n=n+1;
			goto AGAIN;
			}
		else
			{
			printf("\n\n\t\t****** GOOD BYE ******");
			delay(2000);
			exit(1);
			}
		}

Linear Search by Using Function in C

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

	void LINEAER_SEARCH(int DATA[50],int x);

	void TAKING_VALUE(int DATA[50],int x)
		{
		int i;
		printf("START ENTERING VALUE\n");
		for(i=1;i<=x;i++)
			{
			printf("DATA[%d]=",i);
			scanf("%d",&DATA[i]);
			}
		LINEAER_SEARCH(DATA,x);
		}

	void LINEAER_SEARCH(int DATA[50],int x)
		{
		int i,loc,v,k=0;
		printf("ENTER VALUE TO SEARCH:\t");
		scanf("%d",&v);
		for(i=1;i<=x;i++)
			{
			if(DATA[i]==v)
				{
				k=1;
				printf("%d IS AT LOCATION %d",DATA[i],i);
				break;
				}
			}
		if(k==0)
			{
			printf("\n\aSORRY. %d CANN'T FIND",v);
			}
		}

	void main()
		{
		int i,n,DATA[50];
		char c;
		clrscr();
		printf("HOW MANY ELEMENT YOU WANT:\t");
		scanf("%d",&n);
		TAKING_VALUE(DATA,n);
		getch();
		}

Multi-Insert by Using Function in C

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void READ_VALUE(int data[50],int n1)
	{
	int i;
	for(i=1;i<=n1;i++)
		{
		printf("data[%d]= ",i);
		scanf("%d",&data[i]);
		}
	}
void MULTI_INSERT(int data[50],int n1,int l,int n2)
	{
	int i,j,a[50];
	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]);
		}
	}

void main()
	{
	clrscr();
	int n1,n2,l,data[50];
	char c;
	printf("how many element u want:\t");	scanf("%d",&n1);
	READ_VALUE(data,n1);
	again:
	printf("\nEnter location to insert:\t");  scanf("%d",&l);
	printf("\nHow many element u want:\t");   scanf("%d",&n2);
	MULTI_INSERT(data,n1,l,n2);
	printf("\nInsert again ? (y/n)..");
	c=getch();
	if(c=='y')
		{
		n1=n1+n2;
		goto again;
		}
	else
		exit(0);
	}

Normal Insert in C

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<dos.h>
void main()
	{
	int i,n,k,t,DATA[50],loc,v;
	char c;
	clrscr();
	printf("HOW MANY ELEMENT YOU WANT:\t");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		{
		printf("DATA[%d]=",i);	scanf("%d",&DATA[i]);
		}
	INSERT:
	printf("\nENTER LOCATION TO INSERT:\t");  scanf("%d",&loc);
	if(loc>n)
		{
		printf("\n\aSORRY OOCATION IS OUT OF RANGE. TRY AGAIN...");
		goto INSERT;
		}
	printf("\nENTER VALUE:\t");  scanf("%d",&v);
	printf("\nAFTER INSERT..");
	for(i=1;i<=n+1;i++)
		{
		if(loc==i)
			{
			for(k=i;k<=n+1;k++)
				{
				t=DATA[k];
				DATA[k]=v;
				v=t;
				}
			}
		printf("\nDATA[%d]=%d",i,DATA[i]);
		}
	printf("\nINSERT AGAIN ? (y/n)..");
	c=getch();
	if(c=='y'||c=='Y')
		{
		n=n+1;
		goto INSERT;
		}
	else
		{
		printf("\n\n\t\t****** GOOD BEY ******");
		delay(2500);
		exit(0);
		}
	}

Linear Search in C

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
void main()
	{
	int i,n,DATA[50],index,v,l=1;
	char c;
	clrscr();
	printf("How many element you want :\t"); scanf("%d",&n);
	clrscr();
	printf("Start entering value:\n");
	for(i=1;i<=n;i++)
		{
		printf("DATA[%d]=",i);	scanf("%d",&DATA[i]);
		}
	printf("\nDO YOU WANT TO SEARCH ANY DATA.(y/n)");
	c=getch();
	if(c=='y')
		{
		 SEARCH:
		 printf("\nENTER VALUE TO SEARCH: ");scanf("%d",&v);
		 for(i=1;i<=n;i++)
			{
			if(DATA[i]==v)
				{
				l=0;
				index=i;
				printf("\nINDEX NUMBER OF %d IS %d",v,index);
				break;
				}
			}
		   if(l==1)
			{
			printf("\n\aSORRY THERE ISN'T SUCH KIND OF VALUE.TRY AGAIN...");
			goto SEARCH;
			}
		   printf("\nDO IT AGAIN ?(y/n)");
		   c=getch();
		   if(c=='y')
			{
			goto SEARCH;
			}

		}
	else
		exit(1);
	}

Binary Search in C

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

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);
		//READING VALUE//
	for(i=1;i<=n;i++)
		{
		printf("DATA[%d]=",i); scanf("%d",&DATA[i]);
		}
		//START SORTING//
	for(i=1;i<=n-1;i++)
		{
		for(j=i+1;j<=n;j++)
			{
			if(DATA[i]>DATA[j])
				{
				t=DATA[i];
				DATA[i]=DATA[j];
				DATA[j]=t;
				}
			}
		}
	printf("AFTER SORTING..\n");
	for(i=1;i<=n;i++)
		{
		printf("\nDATA[%d]=%d",i,DATA[i]);
		}
	BINARY_SEARCH:
	printf("\nENTER VALUE TO FIND:\t"); scanf("%d",&v);
	beg=1;	end=n;	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");
		}
	printf("\nTRY AGAIN ? (Y/N)..");
	c=getch();
	if(c=='y'||c=='Y')
		{
		goto BINARY_SEARCH;
		}
	else
		exit(0);
	}