w3resource

C# Sharp Exercises: Verify that a string contains valid parentheses

C# Sharp Basic: Exercise-95 with Solution

A given string contains the bracket characters '(', ')', '{', '}', '<', ‘>', '[' and ']',
Write a C# programme to check the said string is valid or not. The input string will be valid when open brackets and closed brackets are same type of brackets.
Or
open brackets will be closed in proper order.

Sample Data:
( "<>") -> True
("<>()[]{}") -> True
("(<>") -> False
("[<>()[]{}]") -> True

Sample Solution-1:

C# Sharp Code:

using System;
using System.Collections.Generic;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "<>";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Verify the said string contains valid parentheses: " + test(text));
            text = "<>()[]{}";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Verify the said string contains valid parentheses: " + test(text));
            text = "(<>";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Verify the said string contains valid parentheses: " + test(text));
            text = "[<>()[]{}]";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Verify the said string contains valid parentheses: " + test(text));
        }
        public static bool test(string s)
        {
            Stack<char> ch = new Stack<char>();
            foreach (var item in s.ToCharArray())
                if (item == '(')
                    ch.Push(')');
                else if (item == '<')
                    ch.Push('>');
                else if (item == '[')
                    ch.Push(']');
                else if (item == '{')
                    ch.Push('}');
                else if (ch.Count == 0 || ch.Pop() != item)
                    return false;

            return ch.Count == 0;
        }
    }
}

Sample Output:

Original string: <>
Verify the said string contains valid parentheses: True
Original string: <>()[]{}
Verify the said string contains valid parentheses: True
Original string: (<>
Verify the said string contains valid parentheses: False
Original string: [<>()[]{}]
Verify the said string contains valid parentheses: True

Flowchart:

Flowchart: C# Sharp Exercises - Verify that a string contains valid parentheses.

Sample Solution-2:

C# Sharp Code:

using System;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "<>";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Verify the said string contains valid parentheses: " + test(text));
            text = "<>()[]{}";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Verify the said string contains valid parentheses: " + test(text));
            text = "(<>";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Verify the said string contains valid parentheses: " + test(text));
            text = "[<>()[]{}]";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Verify the said string contains valid parentheses: " + test(text));
        }
        public static bool test(string text)
        {
            string temp_text = string.Empty;

            while (text != temp_text)
            {
                temp_text = text;
                text = text.Replace("<>", "").Replace("()", "").Replace("[]", "").Replace("{}", "");
            }

            return text == string.Empty ? true : false;
        }
    }
}

Sample Output:

Original string: <>
Verify the said string contains valid parentheses: True
Original string: <>()[]{}
Verify the said string contains valid parentheses: True
Original string: (<>
Verify the said string contains valid parentheses: False
Original string: [<>()[]{}]
Verify the said string contains valid parentheses: True

Flowchart:

Flowchart: C# Sharp Exercises - Verify that a string contains valid parentheses.

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Find the longest common prefix from an array of strings.
Next C# Sharp Exercise: String with same characters .

What is the difficulty level of this exercise?



Share this Tutorial / Exercise on : Facebook and Twitter