Search
 
SCRIPT & CODE EXAMPLE
 

CPP

balanced brackets in c++

#include <iostream>

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] == '(' || input[i] == '{' || input[i] == '[')
        {
            s[top] = input[i];
            top++;
        }else if(input[i] == ')' || input[i] == '}' || input[i] == ']')
        {
            if(top == 0)
            {
                return 0;
            }
            
            top--;
            if(s[top] == ')' || s[top] == '}' || 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 :: has substr c++ 
Cpp :: cpp malloc 
Cpp :: assignment operator with pointers c++ 
Cpp :: tabeau dynamique c++ 
Cpp :: how to print an array in cpp in single line 
Cpp :: STD::ERASE FUNCTION IN C++ 
Cpp :: c++ define array with values 
Cpp :: evennumbers 1 to 100 
Cpp :: string c++ 
Cpp :: first and last digit of a number in c++ 
Cpp :: order 2d array in c++ 
Cpp :: c++ function pointer 
Cpp :: minimum or maximum in array c++ 
Cpp :: c++ check first character of string 
Cpp :: pointer to pointer c++ 
Cpp :: min heap 
Cpp :: Write a C++ program using constructor 
Cpp :: pragma HLS bracets 
Cpp :: fname from FString 
Cpp :: Maximum Pairwise Modular Sum codechef solution in c++ 
Cpp :: end vs cend in cpp 
Cpp :: print all variables separated by comma c++ 
Cpp :: point in polygon 
Cpp :: the number of ones int bitset 
Cpp :: how to open file without override c++ 
Cpp :: cout alternative c++ 
Cpp :: codeforces problem 1700A solution in c++ 
Cpp :: how to point to next array using pointer c++ 
Cpp :: Equalize problem codeforces 
Cpp :: std::ifstream cant read file to large 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =