Search
 
SCRIPT & CODE EXAMPLE
 

CPP

balanced parentheses

#include <iostream>
//This program works only for parantheses "()".
//Check more to get the program of balanced brackets.
using namespace std;

int is_balanced(char input[])
{
    char s[100];
    char last;
    int top = 0;
    
    
    for(int i = 0; input[i] != ''; i++)
    {
        if(input[i] == '(')
        {
            s[top] = '(';
            top++;
        }else if(input[i] == ')')
        {
            if(top == 0)
            {
                return 0;
            }
            
            top--;
            if(s[top] == ')')
            {
                return 0;
            }
        }
    }
    
    if(top == 0)
    {
        return 1;
    }else
    {
        return 0;
    }
    
}

int main()
{
    char input[100];
    cin >> input;
    if(is_balanced(input))
    {
        cout << "Balanced!
";
    }else
    {
        cout << "Imbalanced!
";
    }
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: find vector size in c++ 
Cpp :: square gcode 
Cpp :: what is the meaning of life and everything in the universe 
Cpp :: potato 
Cpp :: how to use for c++ 
Cpp :: add input in c++ 
Cpp :: how to print a word in c++ 
Cpp :: c++ compare type 
Cpp :: c++ online compiler 
Cpp :: has substr c++ 
Cpp :: backtrack 
Cpp :: c++ memset 
Cpp :: C++ Nested if 
Cpp :: web dev c++ 
Cpp :: Temparory Email Id 
Cpp :: priority queue in cpp 
Cpp :: how to rotate a matrix 90 degrees clockwise 
Cpp :: know what the input data is whether integer or not 
Cpp :: print all subsequences 
Cpp :: Write a C++ program using constructor 
Cpp :: visual studio code terminal keeps closing c++ 
Cpp :: C++ Initialization of three-dimensional array 
Cpp :: prefix using stack 
Cpp :: code runner c++ syntax error 
Cpp :: how does sorting array works in c++ 
Cpp :: prompt user for bool statement C++ 
Cpp :: cpp module 42 
Cpp :: pointers mcq sanfoundry 
Cpp :: check whether kth bit is 1 
Cpp :: how to open program in c++ 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =