Program to Remove brackets from an algebraic expression

Program to Remove brackets from an algebraic expression

21 April 2023

21 April 2023

Write a Program to Remove brackets from an algebraic expression



If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023

Description

Get an algebraic expression as input from the user and then remove all the brackets in that.

Input

7x+(2*y)

Output

7x+2*y

 

C Program

#include<stdio.h>

#include<string.h>

int main()

{

    char str1[50], str2[50];

    printf("Enter a string: ");

    scanf("%s",str1);

    int i=0, j=0 ;

    while(str1[i] != '\0')

    {

        if(str1[i] != '(' &&  str1[i] != ')')

        {

            str2[j++] = str1[i];

        }

        i++;

    }

    str2[j] = '\0';

    printf("%s", str2);

    return 0;

}

 

C++ Program

#include<iostream>

#include<string.h>

using namespace std;

int main()

{

    char str1[50], str2[50];

    cout<<"Enter a string: ";

    cin>>str1;

    int i=0, j=0 ;

    while(str1[i] != '\0')

    {

        if(str1[i] != '(' &&  str1[i] != ')')

        {

            str2[j++] = str1[i];

        }

        i++;

    }

    str2[j] = '\0';

    cout<<str2;

    return 0;

}

 

Java Program

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

     Scanner sc = new Scanner(System.in);

     System.out.print("Enter algebraic expression: ");

     String str = sc.nextLine();

     String newstr = str.replaceAll("[(){}]","");

     System.out.println("Expression without brackets : "+newstr);

  }

}

 

Python Program

str1=input('Enter an algebraic equation: ')

str2=''

for i in str1:

    if ord(i) == 41 or ord(i) == 40 or ord(i) == 91 or ord(i) == 93 or ord(i) == 123 or ord(i) == 125:

        pass

    else:

        str2=str2+i

print(str2)


If you are from 2023 batch student, Join our Telegram group for placement preparation and coming placement drive updates : https://t.me/talentbattle2023


Related Articles

Ask Us Anything !