Java program to remove brackets from an algebraic expression

Remove brackets from an algebraic expression

In this program, we’re going to remove brackets from an algebraic expression. Take a String input from the user and store it in the variable called “s”. After that use replaceAll() method which was present in string class the work of replaceAll() method is to replace some old thing with some new thing so here we’re using a regular expression to replace brackets with white spaces from an algebraic expression like s.replaceAll(“[(){}]”,””) and after that simply print new expression which doesn’t have any brackets. Algorithm

• First take String input from user and Store it in the variable called as “s”.
• After that use replaceAll() method .
• Use regular expression to replace brackets present in algebraic expression with whitespaces.
• Atlast simply print that expression after removing brackets.

Code in Java

import java.util.Scanner;

public class RemoveBracketsFromAnAlgebraicString {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter Some Expression : ");
String s = sc.nextLine();
String result = s.replaceAll("[(){}]","");
System.out.println("Expression without brackets : "+result);
}
}
This code is contributed by Shubham Nigam (Prepinsta Placement Cell Student)

Output

Enter Some Expression :(a+b)=c
Expression without brackets : a+b=c

3 comments on “Java program to remove brackets from an algebraic expression”

• Surya

#python code

string1=input()
string2=str()
for i in string1:
if (i==”)” or i==”(“):
pass
else:
string2=string2+i
print(string2) 0
• NITIN

In Python:-
n = input()
a = ”
for i in n:
if i == ‘(‘ or i == ‘)’ or i== ‘[‘ or i==’]’ or i== ‘{‘ or i== ‘}’ :
continue
else:a = a+i
print(a) 0
• S

s1=input()
s2=str()
for i in s1:
if i==”(“or i==”)”:
pass
else:
s2=s2+i
print(s2)
in python 0