Untitled

 avatar
unknown
plain_text
2 years ago
1.5 kB
12
Indexable
#include <iostream>
#include <fstream>
bool balance_parenthesis_helper(char* str, int index, int count);

int main()
{
    std::fstream txet;
    txet.open("txet",std::ios::in);
    if (!txet)
    {
        std::cout<<"File created faild"<<std::endl;
        return NULL;
    }
    else
    {
        std::cout<<"File is open for reading"<<std::endl;
        txet.seekg(0, txet.end);
        int size = txet.tellg();
        txet.seekg(0, txet.beg);
        char* str = new char[size + 1];
        if (!str)
        {
            std::cout<<"Error -> Out Memory"<<std::endl;
            return NULL;
        }
        txet.read(str, size);
        str[size] = '\0';
        std::cout<<str;
        if (balance_parenthesis_helper(str, 0, 0))
        {
            std::cout<<"The string is balanced"<<std::endl;
        }
        else
        {
            std::cout<<"The string is not balanced"<<std::endl;
        }
        delete[] str;
    }
            txet.close();
            //delete[] str;
            return 0;
    
}


bool balance_parenthesis_helper(char* str, int index, int count)
{
    if (str[index] == '\0')
    {
      if (count == 0)
     {
       return true;
     }
      return false;
    }
    if (str[index] == '(' || str[index] == '[' || str[index] == '{')
    {
      count++;
    }
    else if (str[index] == ')' || str[index] == ']' || str[index] == '}')
    {
       count--;
      if (count < 0)
     {
       return false;
     }
    }
    return balance_parenthesis_helper(str, index + 1, count);
}
Editor is loading...