Parenthesis C# Exercise - C# Programming Course

 Exercise

Parenthesis

 Objetive

Implement a function to check if a sequence of open and closed parentheses is balanced. In other words, check if each open parenthesis corresponds to a closing one and they are correctly nested.

For example:

"(()()(()))" is OK
"(((()" is an ERROR

 Example Code

// Importing necessary namespace for handling collections
using System;  // Basic namespace for console input/output and fundamental operations
using System.Collections.Generic;  // For using the Stack class

class Program
{
    // Function to check if a sequence of parentheses is balanced
    static bool IsBalanced(string parentheses)
    {
        // Create a stack to hold the open parentheses
        Stack stack = new Stack();  // Stack will help to track unmatched open parentheses

        // Iterate through each character in the input string
        foreach (char ch in parentheses)  // For each character in the string
        {
            // If the character is an opening parenthesis, push it onto the stack
            if (ch == '(')  // If the character is an open parenthesis
            {
                stack.Push(ch);  // Add the open parenthesis to the stack
            }
            // If the character is a closing parenthesis, check if there's a matching open parenthesis
            else if (ch == ')')  // If the character is a closing parenthesis
            {
                // If the stack is empty, it means no matching open parenthesis, return false
                if (stack.Count == 0)  // If there are no open parentheses to match with
                {
                    return false;  // It means the parentheses are not balanced
                }
                stack.Pop();  // Remove the top item from the stack (matching the open parenthesis)
            }
        }

        // If the stack is empty, all open parentheses had matching closing parentheses
        return stack.Count == 0;  // If the stack is empty, the parentheses are balanced
    }

    // Main program method to test the IsBalanced function
    static void Main(string[] args)
    {
        // Test the IsBalanced function with valid and invalid examples
        string test1 = "(()()(()))";  // A valid sequence of parentheses
        string test2 = "(((()";  // An invalid sequence of parentheses

        // Display the results of the balance check
        Console.WriteLine($"Is the sequence '{test1}' balanced? {IsBalanced(test1)}");  // Expecting true
        Console.WriteLine($"Is the sequence '{test2}' balanced? {IsBalanced(test2)}");  // Expecting false
    }
}

More C# Exercises of Dynamic Memory Management

 Implementing a queue using array
Implementing a queue...
 Implementing a stack using array
Implementing a stack...
 Queue Collections
Create a string queue using the Queue class that already exists in the DotNet platform....
 Queue Stack Reverse Polish Notation
Create a program that reads a Reverse Polish Notation expression from a text file, for example: 3 4 6 5 - + * 6 + (Result 21) Each item will be...
 ArrayList
Create a string list using the ArrayList class that already exists in the .NET platform. Once created, display all the items stored in the list. In...
 ArrayList duplicate a text file
Create a program that reads from a text file and stores it to another text file by reversing the order of lines. For example, an input text file li...
 Unlimited sum
Create a program to allow the user to enter an unlimited amount of numbers. Also, they can enter the following commands: "sum", to display the sum of...
 ArrayList - Text file reader
provide your basic text file reader here, which displays 21 lines of text and allows the user to navigate using the up and down arrow keys, and exit u...
 Hast Table - Dictionary
Submit your dictionary here using a hash table....
 Mix and sort files
Create a program that reads the contents of two different files, merges them, and sorts them alphabetically. For example, if the files contain: "Dog C...
 ArrayList of Points
Create a structure named "Point3D" to represent a point in 3D space with coordinates X, Y, and Z. Create a program that has a menu where the user c...
 Search in file
Create a program that reads a text file, saves its content to an ArrayList, and asks the user to enter sentences to search within the file. The pro...

Juan A. Ripoll - Programming Tutorials and Courses © 2025 All rights reserved.  Legal Conditions.