Skip to content

Commit

Permalink
Merge pull request akkupy#451 from ROHITTTTZ/rohittttz
Browse files Browse the repository at this point in the history
Create Paranthesis Checker using stack in C [Hacktober2023]
  • Loading branch information
akkupy authored Oct 1, 2023
2 parents aa8c812 + c08d68f commit a4a249f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 16 deletions.
56 changes: 56 additions & 0 deletions c/paranthesisChecker.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include<stdio.h>
#include<conio.h>
#include<string.h>
int top=-1;
char stack[10];

void push(char ch){
if(top==9)
printf("Stack is full");
else
stack[++top]=ch;
}

char pop(){
char c1;
if(top==-1)
printf("stack is empty");
else
c1=stack[top--];
return c1;
}

//Paranthesis checker using Stack

void main(){
char exp[10],tp;
int i,flag=1;
clrscr();
printf("Enter the expression: ");
gets(exp);
for(i=0;i<strlen(exp);i++){
if(exp[i]=='('||exp[i]=='{'||exp[i]=='['){
push(exp[i]);
}
if(exp[i]==')'||exp[i]=='}'||exp[i]==']'){
if(top==-1)
flag=0;
else
tp=pop();
}
if(exp[i]==')'&&(tp=='{'||tp=='['))
flag=0;
if(exp[i]=='}'&&(tp=='('||tp=='['))
flag=0;
if(exp[i]==']'&&(tp=='{'||tp=='('))
flag=0;
}
if(top>=0)
flag=0;
if(flag==1)
printf("match");
else
printf("Mismatch");

getch();
}
30 changes: 14 additions & 16 deletions cpp/binary_Search.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
#include <iostream>
#include <vector>

#include <bits/stdc++.h>
using namespace std;

int binarySearch(const vector<int>& arr, int target) {

// Binary search function
int binary_search(const vector<int>& arr, int tar) {
int left = 0;
int right = arr.size() - 1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid; // Target found, return its index
} else if (arr[mid] < target) {
left = mid + 1; // Adjust the left boundary
if (arr[mid] == tar) {
return mid;
} else if (arr[mid] < tar) {
left = mid + 1;
} else {
right = mid - 1; // Adjust the right boundary
right = mid - 1;
}
}

return -1; // Target not found in the array
return -1; // Element not found
}

int main() {
vector<int> arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int target = 5;
int tar = 5;

int result = binarySearch(arr, target);
int result = binary_search(arr, tar);

if (result != -1) {
cout << "Element found at index " << result << endl;
cout << "Element " << tar << " found at index " << result << endl;
} else {
cout << "Element not found in the array" << endl;
cout << "Element " << tar << " not found in the given array." << endl;
}

return 0;
Expand Down

0 comments on commit a4a249f

Please sign in to comment.