Horje
balanced parentheses Code Example
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] != '\0'; 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!\n";
    }else
    {
        cout << "Imbalanced!\n";
    }
    return 0;
}
Algorithm check balanced parentheses
if (null == str || ((str.length() % 2) != 0)) {
    return false;
} else {
    char[] ch = str.toCharArray();
    for (char c : ch) {
        if (!(c == '{' || c == '[' || c == '(' || c == '}' || c == ']' || c == ')')) {
            return false;
        }
    }
}




Cpp

Related
subtract two numbers without using arithmetic operators in java Code Example subtract two numbers without using arithmetic operators in java Code Example
expresiones regulares español Code Example expresiones regulares español Code Example
are strings mutable in c++ Code Example are strings mutable in c++ Code Example
resize string c++ Code Example resize string c++ Code Example
time complexity analysis of algorithms calculator Code Example time complexity analysis of algorithms calculator Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
9