Reverse a String using stack.

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

int top = -1;
char stack[MAX];
char pop();
void push(char item);
int main()
{
char str[20];
int i;
printf(“Enter the string : ” );
gets(str);

for(i=0;i<strlen(str);i++)
push(str[i]);
for(i=0;i<strlen(str);i++)
str[i]=pop();
printf(“Reversed string is : “);
puts(str);
getch();
return 0;
}

void push(char item)
{
if(top == (MAX-1))
printf(“Stack Overflow\n”);
else
stack[++top] =item;
}

char pop()
{
if(top == -1)
printf(“Stack Underflow\n”);
else
return stack[top--];
}



Binary Search

#include <stdio.h>

#include <stdlib.h>

#include<conio.h>

int main()

{ int num,a[50],size,middle,start,end,i;

printf(“\nEnter the Number Of Elements You Want To Enter :: “);

scanf(“%d”,&size);

printf(“\nEnter the Elements Of The Array :: “);

for(i=0;i<size;i++)

{ scanf(“%d”,&a[i]);

}

printf(“\nEnter the Element You Want To Search :: “);

scanf(“%d”,&num);

start = 0 ;

end = size-1;

middle = (start+end)/2;

while(start<=end)

{ if(num == a[middle])

{

printf(“\n The Element Is Found at %d”,middle + 1);

break;

}

else if (num > a[middle])

{

start = middle + 1;

middle = (start+end)/2;

}

else if(num < a[middle])

{

end = middle-1;

middle = (start+end)/2;

}

else

{

printf(“\nElement Not Found !!! “);

}

}

getch();

}


Linear Search

#include <stdio.h>

#include <stdlib.h>

#include<conio.h>



int main()

{

int i,num,a[50],size;

printf(“\nEnter the Number Of Elements You Want To Enter :: “);

scanf(“%d”,&size);

printf(“\nEnter the Elements Of The Array :: “);

for(i=0;i<size;i++)

{

scanf(“%d”,&a[i]);

}

printf(“\nEnter the Element You Want To Search :: “);

scanf(“%d”,&num);

for(i=0;i<size;i++)

{

if(a[i] == num)

{

printf(“\nYor Element %d Is Found At Position %d “,num,i+1);

}

}

getch();

}