Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7738991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:24:33+00:00 2026-06-01T08:24:33+00:00

my program should print a message on the screen if the formula that the

  • 0

my program should print a message on the screen if the formula that the user entered is good for the terms(you can only use digits and letters, u can’t start with ‘(‘ and like mathematical formula, for each open bracket, has to be a suitable(and in the right place) close bracket.

here some formulas that the program should accepts and prints:

True-

  • a(aa(a)aaa(aa(a)aa)aa)aaaaa
  • a(((())))

here some formulas that the program should not accepts and prints:

False-

  • ()()()
  • )()()(

but the program always prints False
thanks for helping
Heres the code:EDIT

    bool IsNumeric(char character)
    {
        return "0123456789".Contains(character);
        // or return Char.IsNumber(character);
    }

    bool IsLetter(char character)
    {
        return "ABCDEFGHIJKLMNOPQRSTUVWXWZabcdefghigjklmnopqrstuvwxyz".Contains(character);

    }

    bool IsRecognized(char character)
    {
        return IsBracket(character) | IsNumeric(character) | IsLetter(character);
    }
    public bool IsValidInput(string input)
    {
        if (String.IsNullOrEmpty(input) || IsBracket(input[0]))
        {
            return false;
        }
        var bracketsCounter = 0;
        for (var i = 0; i < input.Length; i++)
        {
            var character = input[i];
            if (!IsRecognized(character))
            {
                return false;
            }
            if (IsBracket(character))
            {
                if (character == '(')
                    bracketsCounter++;
                if (character == ')')
                    bracketsCounter--;
            }
        }


        if (bracketsCounter > 0)
        {
            return false;
        }

        return bracketsCounter==0;
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:24:34+00:00Added an answer on June 1, 2026 at 8:24 am

    Is debugging this hard really? This condition:

    ((!IsNumeric(st[i])) && (st[i] != '(') && (st[i] != ')')&&((st[i]<'a')||(st[i]>'z')||(st[i]<'A')||(st[i]>'Z')))
        return false;
    

    is obviously wrong. It returns false for a every time. You don’t take into consideration that a is greater than Z.

    EDIT:

    so how can i make it easier to read? that the only way i figured. do u
    have other solution for this problem?

    As for that condition block – use smaller methods / functions, for example.

    bool IsBracket(char character)
    {
        return (character == '(' | character == ')');
    }
    
    bool IsNumeric(char character)
    {
        return "0123456789".Contains(character);
        // or return Char.IsNumber(character);
    }
    
    bool IsLetter(char character)
    {
        // see why this is NOT prone to fail just because 'a' is greater than 'Z' in C#?
        return (character >= 'a' & character <= 'z') |
            (character >= 'A' & character <= 'Z');
    
        // or return Regex.IsMatch(character.ToString(), "[a-zA-Z]", RegexOptions.None);
        // or return Char.IsLetter(character);
    }
    
    // now you can implement:
    bool IsRecognized(char character)
    {
        return IsBracket(character) | IsNumeric(character) | IsLetter(character);
    }
    

    and then in your big method you could just safely use:

    if (!IsRecognized(st[i]))
        return false;
    

    It may look like an overkill for such a trivial example, but it’s a better approach in principle, and certainly more readable.

    And after that, you could reduce your code to something along the lines of:

        bool IsInputValid(string input)
        {
            if (String.IsNullOrEmpty(input) || IsBracket(input[0]))
            {
                return false;
            }
            var bracketsCounter = 0;
            for (var i = 0; i < input.Length; i++)
            {
                var character = input[i];
                if (!IsRecognized(character))
                {
                    return false;
                }
                if (IsBracket(character)) // redundant?
                {
                    if (character == '(') // then what?
                    if (character == ')') // then what?
                }
                if (bracketsCounter < what?)
                {
                    what?
                }
            }
            return bracketsCounter == what?;
        }
    

    (dasblinkenlight‘s algorithm)

    EDIT 10th April

    You got it wrong.

        bool IsNumeric(char character)
        {
            return "0123456789".Contains(character);
            // or return Char.IsNumber(character);
        }
    
        bool IsLetter(char character)
        {
            return "ABCDEFGHIJKLMNOPQRSTUVWXWZabcdefghigjklmnopqrstuvwxyz".Contains(character);
    
        }
    
        bool IsRecognized(char character)
        {
            return IsBracket(character) | IsNumeric(character) | IsLetter(character);
        }
        public bool IsValidInput(string input)
        {
            if (String.IsNullOrEmpty(input) || IsBracket(input[0]))
            {
                return false;
            }
            var bracketsCounter = 0;
            for (var i = 0; i < input.Length; i++)
            {
                var character = input[i];
                if (!IsRecognized(character))
                {
                    return false;
                }
                if (IsBracket(character))
                {
                    if (character == '(')
                        bracketsCounter++;
                    if (character == ')')
                        bracketsCounter--;
                }
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                if (bracketsCounter < 0) // NOT "> 0", and HERE - INSIDE the for loop
                {
                    return false;
                }
                // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            }
    
            return bracketsCounter==0;
        }
    }
    }
    

    By the way, you also made a mistake in your IsLetter method:
    …UVWXWZ? Should be UVWXYZ

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program isn't running properly as should be... I'm getting only the error message
To print debug messages in my program, I have a that can be used
If a user types in [[0,0,0], [0,0,1], [1,1,0]] and press enter, the program should
I have this program that should execute a piece of code base on the
Im writing a program that should read input via stdin, so I have the
I just want that my program or method should run at specific date and
The perldoc page for length() tells me that I should use bytes::length(EXPR) to find
I have tried a program where if username matches password, welcome message should be
I am writing a gui program that allows a user to repeatedly send a
This program should calculate the value of 2^1+2^2 + ... + 2^10: #include <stdio.h>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.