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;
}
}
}
Is debugging this hard really? This condition:
is obviously wrong. It returns
falseforaevery time. You don’t take into consideration thatais greater thanZ.EDIT:
As for that condition block – use smaller methods / functions, for example.
and then in your big method you could just safely use:
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:
(
dasblinkenlight‘s algorithm)EDIT 10th April
You got it wrong.
By the way, you also made a mistake in your IsLetter method:
…UVWXWZ? Should be UVWXYZ