Write a program that will take a string as input.
The program will then determine whether each
left parenthesis '(' has a matching right
parenthesis). If so, the program will print 0
else the program will print 1.
For example,
For the input provided as follows:
HELLO AND (WELCOME (TO THE) TCEA
(CONTEST)TODAY) IS (SATURDAYO)
The output of the program will be:
0
Another example,
For the input provided as follows:
(9x(7-2)* (15)
The output of the program will be:
1
Answers
Answered by
45
Answer:
please specify the languages also, I am making it in Java and Python
Java
import java.util.Scanner;
class Brakets
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
String n = in.nextLine();
int start = 0;
int end = 0;
for (int i=0 ; i<n.length() ; i++)
{
if (n.charAt(i) == '(')
{
start++;
}
if (n.chatAt(i) == ')')
{
end++;
}
}
if (start == end)
System.out.print(0);
else
System.out.print(1);
}
}
Python
n = input()
start = 0
end = 0
for i in n:
if i == "(":
start += 1
elif i == ")":
end += 1
if end == start:
print(0)
else:
print(1)
Similar questions