C# Open Close Brackets checking


How to check open close brackets without stack.
In this example we checked with ( and [ brackets.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Davaleba2
{
    class Program
    {
        static void Main(string[] args)
        {
            string s, ss, opens = null;
            s = Console.ReadLine();

            if (s.Length % 2 != 0)
            {
                Console.WriteLine("False");
                Console.ReadLine();
            }
            else
            {
                for (int i = 0; i < s.Length; i++)
                {
                    if (s[i] == ')' || s[i] == ']')
                    {
                        int temp;
                        temp = opens.Length;
                        if (temp > 0)
                        {
                            ss = damxurvelisPovna(opens[temp - 1].ToString(), s[i].ToString());
                            if (ss == "True")
                            {
                                string sss = null;
                                for (int j = 0; j < opens.Length - 1; j++)
                                {
                                    sss = sss + opens[j];
                                }
                                opens = sss;
                            }
                            else
                            {
                                Console.WriteLine("False"); Console.ReadLine();
                            }
                        }
                        else
                        {
                            Console.WriteLine("False"); Console.ReadLine();

                        }
                    }
                    if (s[i] == '(' || s[i] == '[')
                    {
                        opens = opens + s[i];
                    }
                }
                if (opens == null) Console.WriteLine("True");
                else Console.WriteLine("False");
                Console.ReadLine();
            }
            string damxurvelisPovna(string source, string destination)
            {
                string res = null;
                if (source == "(" && destination == ")") res = "True";
                else
                if (source == "[" && destination == "]") res = "True";
                else res = "False";
                return res;
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.