The Wayback Machine - https://web.archive.org/web/20240917131959/https://www.geeksforgeeks.org/program-evaluate-simple-expressions/
Open In App

Program to evaluate simple expressions

Last Updated : 03 Nov, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

You are given a string that represent an expression of digits and operands. E.g. 1+2*3, 1-2+4. You need to evaluate the string or the expression. NO BODMAS is followed. If the expression is of incorrect syntax return -1. 
Test cases: 
a) 1+2*3 will be evaluated to 9. 
b) 4-2+6*3 will be evaluated to 24. 
c) 1++2 will be evaluated to -1(INVALID). 
Also, in the string spaces can occur. For that case we need to ignore the spaces. Like :- 1*2 -1 is equals to 1.
Source: Amazon Interview Question
It is strongly recommend to minimize the browser and try this yourself first. 
The idea is simple start from the first character and traverse from left to right and check for errors like two consecutive operators and operands. We also keep track of result and update the result while traversing the expression.
Following is the program to evaluate the given expression. 
 

C++




// C++ program to evaluate a given expression
#include <iostream>
using namespace std;
 
// A utility function to check if a given character is operand
bool isOperand(char c) { return (c >= '0' && c <= '9'); }
 
// utility function to find value of and operand
int value(char c) {  return (c - '0'); }
 
// This function evaluates simple expressions. It returns -1 if the
// given expression is invalid.
int evaluate(char *exp)
{
    // Base Case: Given expression is empty
    if (*exp == '\0')  return -1;
 
    // The first character must be an operand, find its value
    int res = value(exp[0]);
 
    // Traverse the remaining characters in pairs
    for (int i = 1; exp[i]; i += 2)
    {
        // The next character must be an operator, and
        // next to next an operand
        char opr = exp[i], opd = exp[i+1];
 
        // If next to next character is not an operand
        if (!isOperand(opd))  return -1;
 
        // Update result according to the operator
        if (opr == '+')       res += value(opd);
        else if (opr == '-')  res -= value(opd);
        else if (opr == '*')  res *= value(opd);
        else if (opr == '/')  res /= value(opd);
 
        // If not a valid operator
        else                  return -1;
    }
    return res;
}
 
// Driver program to test above function
int main()
{
    char expr1[] = "1+2*5+3";
    int res = evaluate(expr1);
    (res == -1)? cout << expr1 << " is " << "Invalid\n":
                 cout << "Value of " << expr1 << " is " << res << endl;
 
    char expr2[] = "1+2*3";
    res = evaluate(expr2);
    (res == -1)? cout << expr2 << " is " << "Invalid\n":
                 cout << "Value of " << expr2 << " is " << res << endl;
 
    char expr3[] = "4-2+6*3";
    res = evaluate(expr3);
    (res == -1)? cout << expr3 << " is " << "Invalid\n":
                 cout << "Value of " << expr3 << " is " << res << endl;
 
    char expr4[] = "1++2";
    res = evaluate(expr4);
    (res == -1)? cout << expr4 << " is " << "Invalid\n":
                 cout << "Value of " << expr4 << " is " << res << endl;
    return 0;
}


Java




// Java program to evaluate a given expression
 
class GFG{
// A utility function to check if
// a given character is operand
static boolean isOperand(char c)
{
    return (c >= '0' && c <= '9');
     
}
 
// utility function to find value of and operand
static int value(char c)
{
    return (int)(c - '0');
     
}
 
// This function evaluates simple expressions.
// It returns -1 if the given
// expression is invalid.
static int evaluate(String exp)
{
    // Base Case: Given expression is empty
    if (exp.length() == 0) return -1;
 
    // The first character must be
    // an operand, find its value
    int res = value(exp.charAt(0));
 
    // Traverse the remaining characters in pairs
    for (int i = 1; i<exp.length(); i += 2)
    {
        // The next character must be an operator, and
        // next to next an operand
        char opr = exp.charAt(i), opd = exp.charAt(i+1);
 
        // If next to next character is not an operand
        if (isOperand(opd) == false) return -1;
 
        // Update result according to the operator
        if (opr == '+') res += value(opd);
        else if (opr == '-') res -= value(opd);
        else if (opr == '*') res *= value(opd);
        else if (opr == '/') res /= value(opd);
 
        // If not a valid operator
        else                 return -1;
    }
    return res;
}
 
// Driver program to test above function
public static void main(String[] args)
{
    String expr1 = "1+2*5+3";
    int res = evaluate(expr1);
    if(res == -1) System.out.println(expr1+" is Invalid");
    else     System.out.println("Value of "+expr1+" is "+res);
 
    String expr2 = "1+2*3";
    res = evaluate(expr2);
    if(res == -1) System.out.println(expr2+" is Invalid");
    else         System.out.println("Value of "+expr2+" is "+res);
 
    String expr3 = "4-2+6*3";
    res = evaluate(expr3);
    if(res == -1) System.out.println(expr3+" is Invalid");
    else         System.out.println("Value of "+expr3+" is "+res);
 
    String expr4 = "1++2";
    res = evaluate(expr4);
    if(res == -1) System.out.println(expr4+" is Invalid");
    else         System.out.println("Value of "+expr4+" is "+res);
}
}
// This code is contributed by mits


Python3




# Python3 program to evaluate a
# given expression
 
# A utility function to check if
# a given character is operand
def isOperand(c):
  
    return (c >= '0' and c <= '9');
 
# utility function to find
# value of and operand
def value(c):
    return ord(c) - ord('0');
 
# This function evaluates simple
# expressions. It returns -1 if the
# given expression is invalid.
def evaluate(exp):
 
    len1 = len(exp);
     
    # Base Case: Given expression is empty
    if (len1 == 0):
        return -1;
 
    # The first character must be
    # an operand, find its value
    res = value(exp[0]);
 
    # Traverse the remaining
    # characters in pairs
    for i in range(1,len1,2):
        # The next character must be
        # an operator, and next to
        # next an operand
        opr = exp[i];
        opd = exp[i + 1];
 
        # If next to next character
        # is not an operand
        if (isOperand(opd)==False):
            return -1;
 
        # Update result according
        # to the operator
        if (opr == '+'):
            res += value(opd);
        elif (opr == '-'):
            res -= int(value(opd));
        elif (opr == '*'):
            res *= int(value(opd));
        elif (opr == '/'):
            res /= int(value(opd));
 
        # If not a valid operator
        else:
            return -1;
     
    return res;
 
# Driver Code
expr1 = "1+2*5+3";
res = evaluate(expr1);
print(expr1,"is Invalid") if (res == -1) else print("Value of",expr1,"is",res);
 
expr2 = "1+2*3";
res = evaluate(expr2);
print(expr2,"is Invalid") if (res == -1) else print("Value of",expr2,"is",res);
 
expr3 = "4-2+6*3";
res = evaluate(expr3);
print(expr3,"is Invalid") if (res == -1) else print("Value of",expr3,"is",res);
 
expr4 = "1++2";
res = evaluate(expr4);
print(expr4,"is Invalid") if (res == -1) else print("Value of",expr4,"is",res);
 
# This code is contributed by mits


C#




// C# program to evaluate a given expression
using System;
class GFG{
     
// A utility function to check if
// a given character is operand
static bool isOperand(char c) {
    return (c >= '0' && c <= '9');
     
}
 
// utility function to find value of and operand
static int value(char c) { return (int)(c - '0'); }
 
// This function evaluates simple
// expressions. It returns -1 if the
// given expression is invalid.
static int evaluate(string exp)
{
    // Base Case: Given expression is empty
    if (exp.Length == 0) return -1;
 
    // The first character must be
    // an operand, find its value
    int res = value(exp[0]);
 
    // Traverse the remaining characters in pairs
    for (int i = 1; i<exp.Length; i += 2)
    {
        // The next character must be an operator, and
        // next to next an operand
        char opr = exp[i], opd = exp[i+1];
 
        // If next to next character is not an operand
        if (isOperand(opd)==false) return -1;
 
        // Update result according to the operator
        if (opr == '+') res += value(opd);
        else if (opr == '-') res -= value(opd);
        else if (opr == '*') res *= value(opd);
        else if (opr == '/') res /= value(opd);
 
        // If not a valid operator
        else                 return -1;
    }
    return res;
}
 
// Driver program to test above function
static void Main()
{
    string expr1 = "1+2*5+3";
    int res = evaluate(expr1);
    if(res == -1)
    Console.WriteLine(expr1+" is Invalid");
    else    
    Console.WriteLine("Value of "+expr1+" is "+res);
 
    string expr2 = "1+2*3";
    res = evaluate(expr2);
    if(res == -1)
    Console.WriteLine(expr2+" is Invalid");
    else        
    Console.WriteLine("Value of "+expr2+" is "+res);
 
    string expr3 = "4-2+6*3";
    res = evaluate(expr3);
    if(res == -1)
    Console.WriteLine(expr3+" is Invalid");
    else        
    Console.WriteLine("Value of "+expr3+" is "+res);
 
    string expr4 = "1++2";
    res = evaluate(expr4);
    if(res == -1)
    Console.WriteLine(expr4+" is Invalid");
    else        
    Console.WriteLine("Value of "+expr4+" is "+res);
}
}
// This code is contributed by mits


PHP




<?php
// PHP program to evaluate a
// given expression
 
// A utility function to check if
// a given character is operand
function isOperand($c)
{
    return ($c >= '0' && $c <= '9');
}
 
// utility function to find
// value of and operand
function value($c)
{
    return ($c - '0');
}
 
// This function evaluates simple
// expressions. It returns -1 if the
// given expression is invalid.
function evaluate($exp)
{
    $len = strlen($exp);
     
    // Base Case: Given expression is empty
    if ($len == 0) return -1;
 
    // The first character must be
    // an operand, find its value
    $res = (int)(value($exp[0]));
 
    // Traverse the remaining
    // characters in pairs
    for ($i = 1; $i < $len; $i += 2)
    {
        // The next character must be 
        // an operator, and next to
        // next an operand
        $opr = $exp[$i];
        $opd = $exp[$i + 1];
 
        // If next to next character
        // is not an operand
        if (!isOperand($opd))
        return -1;
 
        // Update result according
        // to the operator
        if ($opr == '+')    
        $res += value($opd);
        else if ($opr == '-')
            $res -= (int)(value($opd));
        else if ($opr == '*')
            $res *= (int)(value($opd));
        else if ($opr == '/')
            $res /= (int)(value($opd));
 
        // If not a valid operator
        else               
        return -1;
    }
    return $res;
}
 
// Driver Code
$expr1 = "1+2*5+3";
$res = evaluate($expr1);
($res == -1) ? print($expr1." is Invalid\n"):
               print("Value of " . $expr1 .
                     " is " . $res . "\n");
             
$expr2 = "1+2*3";
$res = evaluate($expr2);
($res == -1) ? print($expr2." is Invalid\n"):
               print("Value of " . $expr2 .
                     " is " . $res . "\n");
             
$expr3 = "4-2+6*3";
$res = evaluate($expr3);
($res == -1) ? print($expr3." is Invalid\n"):
               print("Value of " . $expr3 .
                     " is " . $res . "\n");
             
$expr4 = "1++2";
$res = evaluate($expr4);
($res == -1) ? print($expr4." is Invalid\n"):
               print("Value of " . $expr4 .
                     " is " . $res . "\n");
 
// This code is contributed by mits
?>


Javascript




<script>
// Javascript program to evaluate a given expression
 
// A utility function to check if
// a given character is operand
function isOperand(c)
{
    return (c.charCodeAt(0) >= '0'.charCodeAt(0) && c.charCodeAt(0) <= '9'.charCodeAt(0));
}
 
// utility function to find value of and operand
function value(c)
{
    return (c.charCodeAt(0) - '0'.charCodeAt(0));
}
 
// This function evaluates simple expressions.
// It returns -1 if the given
// expression is invalid.
function evaluate(exp)
{
    // Base Case: Given expression is empty
    if (exp.length == 0) return -1;
   
    // The first character must be
    // an operand, find its value
    let res = value(exp[0]);
   
    // Traverse the remaining characters in pairs
    for (let i = 1; i<exp.length; i += 2)
    {
        // The next character must be an operator, and
        // next to next an operand
        let opr = exp[i], opd = exp[i+1];
   
        // If next to next character is not an operand
        if (isOperand(opd) == false) return -1;
   
        // Update result according to the operator
        if (opr == '+') res += value(opd);
        else if (opr == '-') res -= value(opd);
        else if (opr == '*') res *= value(opd);
        else if (opr == '/') res /= value(opd);
   
        // If not a valid operator
        else                 return -1;
    }
    return res;
}
 
// Driver program to test above function
let expr1 = "1+2*5+3";
let res = evaluate(expr1);
if(res == -1) document.write(expr1+" is Invalid<br>");
else     document.write("Value of "+expr1+" is "+res+"<br>");
 
let expr2 = "1+2*3";
res = evaluate(expr2);
if(res == -1) document.write(expr2+" is Invalid<br>");
else         document.write("Value of "+expr2+" is "+res+"<br>");
 
let expr3 = "4-2+6*3";
res = evaluate(expr3);
if(res == -1) document.write(expr3+" is Invalid<br>");
else         document.write("Value of "+expr3+" is "+res+"<br>");
 
let expr4 = "1++2";
res = evaluate(expr4);
if(res == -1) document.write(expr4+" is Invalid<br>");
else         document.write("Value of "+expr4+" is "+res+"<br>");
 
 
// This code is contributed by avanitrachhadiya2155
</script>


Output: 
 

Value of 1+2*5+3 is 18
Value of 1+2*3 is 9
Value of 4-2+6*3 is 24
1++2 is Invalid

Time Complexity: O(|exp|)

Auxiliary Space: O(1)

The above code doesn’t handle spaces. We can handle spaces by first removing all spaces from the given string. A better solution is to handle spaces in single traversal. This is left as an exercise.
Time Complexity is O(n) where n is length of the given expression.

 



Previous Article
Next Article

Similar Reads

Print all possible expressions that evaluate to a target
Given a string that contains only digits from 0 to 9, and an integer value, target. Find out how many expressions are possible which evaluate to target using binary operator +, - and * in given string of digits. Input : "123", Target : 6 Output : {“1+2+3”, “1*2*3”} Input : “125”, Target : 7 Output : {“1*2+5”, “12-5”} This problem can be solved by p
11 min read
Program to evaluate the expression (&radic;X+1)^6 + (&radic;X-1)^6
Given a number [Tex]X [/Tex]. The task is to find the value of the below expression for the given value of [Tex]X [/Tex]. [Tex](\sqrt[]{X} +1)^6 + (\sqrt[]{X}-1)^6[/Tex] Examples: Input: X = ?2 Output: 198 Explanation: [Tex]2[(\sqrt[]{2})^6 + 15 (\sqrt[]{2})^4 + 15\sqrt[]{2})^2 + 1] [/Tex]= 198Input: X = 3 Output: 4160 Approach: The idea is to use
3 min read
Evaluate the expression ( N<sup>1</sup> * (N - 1)<sup>2</sup> * ... * 1<sup>N</sup>) % (10<sup>9</sup> + 7)
Given an integer N, the task is to find the value of the expression ( N1 * (N - 1)2 * ... * 1N) % (109 + 7). Input: N = 1 Output: 1 Explanation: 11 = 1 Input: N = 4 Output: 288 Explanation: 41 * (4 - 1)2 * (4 - 2)3 * (4-3)4 = 4 * 9 * 8 * 1 = 288 Naive Approach: The simplest approach to solve this problem is to iterate over the range [1, N]. For eve
6 min read
Evaluate an array expression with numbers, + and -
Given an array arr[] of string type which consists of strings "+", "-" and Numbers. Find the sum of the given array. Examples : Input : arr[] = {"3", "+", "4", "-", "7", "+", "13"} Output : Value = 13 The value of expression 3+4-7+13 is 13. Input : arr[] = { "2", "+", "1", "-8", "+", "13"} Output : Value = 8 Approach : First of all, initialize the
6 min read
Evaluate a boolean expression represented as string
Given a string consisting of only 0, 1, A, B, C where A = AND B = OR C = XOR Calculate the value of the string assuming no order of precedence and evaluation is done from left to right.Constraints – The length of string will be odd. It will always be a valid string. Example, 1AA0 will not be given as an input. Examples: Input : 1A0B1 Output : 1 1 A
7 min read
Evaluate the Fractions
Given a 2D array fractions[] and an array value [ ], which denotes that the value of the [Tex]fractions[i][0]/fractions[i][1] = values[i] [/Tex] for each index 'i', you have Q queries of the form [var1, var2]. Print the value of var1/var2 if it can be determined or else print -1. You may assume that there won't be any contradicting equations and th
13 min read
Program to find simple moving average
Simple Moving Average is the average obtained from the data for some t period of time . In normal mean, it's value get changed with the changing data but in this type of mean it also changes with the time interval . We get the mean for some period t and then we remove some previous data . Again we get new mean and this process continues . This is w
6 min read
Simple Input/Output Program in MATLAB
Let us see how to input and output data in MATLAB. input() Syntax : input(PROMPT, "s") Parameters : PROMPT : text prompted "s" : optional, to input a string Returns : the data entered The input() function is used to input data in MATLAB. Example : % entering an integer input(&quot;Enter an integer : &quot;) % entering a string input(&quot;Enter a s
1 min read
Program to find simple moving average | Set-2
Simple Moving Average is the average obtained from the data for some t period of time . In normal mean, its value get changed with the changing data but in this type of mean it also changes with the time interval. We get the mean for some period t and then we remove some previous data. Again we get new mean and this process continues. This is why i
5 min read
Program to find simple interest
What is 'Simple Interest'? Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments. Simple Interest formula: Simple interest formula is given by: Simple Interest = (P x T x R)/100 Where, P
4 min read
JavaScript Program to find simple interest
What is ‘Simple Interest’?Simple interest is a quick method of calculating the interest charge on a loan. Simple interest is determined by multiplying the daily interest rate by the principal by the number of days that elapse between payments. Simple Interest formula:Simple interest formula is given by: Simple Interest = (P x T x R)/100 Where, P is
2 min read
Extracting email addresses using regular expressions in Python
Let suppose a situation in which you have to read some specific data like phone numbers, email addresses, dates, a collection of words etc. How can you do this in a very efficient manner?The Best way to do this by Regular Expression. Let take an example in which we have to find out only email from the given input by Regular Expression. Examples: In
3 min read
Regular Expressions to Validate Google Analytics Tracking Id
Given some Google Analytics Tracking IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid Tracking Id are: It is an alphanumeric string i.e., containing digits (0-9), alphabets (A-Z), and a Special character hyphen(-).The hyphen will come in between the given Google Analytics Tracking Id.Google Analytics
5 min read
Balanced expressions such that given positions have opening brackets | Set 2
Given an integer n and an array of positions ‘position[]’ (1 &lt;= length(position[]) &lt;= 2n), find the number of ways of proper bracket expressions that can be formed of length 2n such that given positions have the opening bracket. Note: position[] array is given in the form of (1-based indexing) [0, 1, 1, 0]. Here 1 denotes the positions at whi
15+ min read
Number of balanced bracket expressions that can be formed from a string
Given a string str comprising of characters (, ), {, }, [, ] and ?. The task is to find the total number of balanced bracket expressions formed when ? can be replaced with any of the bracket characters. Here are some examples of balanced bracket expressions: {([])}, {()}[{}] etc. And, unbalanced bracket expressions: {[}, {()], {()}[) etc. Examples:
12 min read
Check if the given Binary Expressions are valid
Given n expressions of the type x = y and x != y where 1 ? x, y ? n, the task is to check whether the integers from 1 to n can be assigned to x and y such that all the equations are satisfied. Examples: Input: x[] = {1, 2, 3}, op[] = {"=", "=", "!="}, y[] = {2, 3, 1} Output: Invalid If 1 = 2 and 2 = 3 then 3 must be equal to 1. Input: x[] = {1, 2},
10 min read
How to validate a Username using Regular Expressions in Java
Given a string str which represents a username, the task is to validate this username with the help of Regular Expressions. A username is considered valid if all the following constraints are satisfied: The username consists of 6 to 30 characters inclusive. If the username consists of less than 6 or greater than 30 characters, then it is an invalid
3 min read
Print all the possible arithmetic expressions for a given number
Given an integer N, the task is to print all the possible arithmetic expressions using all numbers from 1 to N and with binary operator +, –, * and /.Examples: Input: n = 2 Output: 1+2, 1-2, 1/2, 1*2Input: n = 3 Output: 1+2+3, 1+2-3, 1+2/3, 1+2*3, 1-2+3, 1-2-3, 1-2/3, 1-2*3 1/2+3, 1/2-3, 1/2/3, 1/2*3, 1*2+3, 1*2-3, 1*2/3, 1*2*3 Approach: We will cr
5 min read
Sum of all possible expressions of a numeric string possible by inserting addition operators
Given a numeric string str of length N, the task is to find the sum of all possible expressions by inserting the '+' operator between the characters of the string any number of times. Examples: Input: str = "125" Output: 176 Explanation: Inserting "+" after 1st index modifies str to "1+25" and value = 26 Inserting "+" after 2nd index modifies str t
8 min read
Regular Expressions to Validate Provident Fund(PF) Account Number
Given some PF(Provident Fund) Account Number, the task is to check if they are valid or not using regular expressions. Rules for the valid PF Account Number are : PF account number is alphanumeric String and forward slaces.First five characters are reserved for alphabet letters.Next 17 characters are reserved for digits(0-9).It allows only special
5 min read
Count occurrences of a word in string | Set 2 (Using Regular Expressions)
Given a string str and a word w, the task is to print the number of the occurrence of the given word in the string str using Regular Expression. Examples: Input: str = "peter parker picked a peck of pickled peppers”, w = "peck"Output: 1Explanation: There is only one occurrence of the word "peck" in the given string. Therefore, the output is 1. Inpu
4 min read
US currency validation using Regular Expressions
Given some US Currencies, the task is to check if they are valid or not using regular expressions. Rules for the valid Currency are: It should always start with "$".It can contain only digits (0 - 9) and at most one dot.It should not contain any whitespaces and alphabets.Comma Separator (', ') should be there after every three digits interval. Exam
5 min read
Extracting PAN Number from GST Number Using Regular Expressions
Given a string str in the form of a GST Number, the task is to extract the PAN Number from the given string. General Format of a GST Number: "22AAAAA0000A1Z5" 22: State CodeAAAAA0000A: Permanent Account Number (PAN)1: Entity Number of the same PANZ: Alphabet Z by default5: Checksum digit Examples: Input: str="23BOSPC9911R2Z5Output: BOSPC9911R Input
4 min read
How to validate ISIN using Regular Expressions
ISIN stands for International Securities Identification Number. Given string str, the task is to check whether the given string is a valid ISIN(International Securities Identification Number) or not by using Regular Expression. The valid ISIN(International Securities Identification Number) must satisfy the following conditions: It Should be the com
6 min read
Validating UPI IDs using Regular Expressions
Given some UPI IDs, the task is to check if they are valid or not using regular expressions. Rules for the valid UPI ID: UPI ID is an alphanumeric String i.e., formed using digits(0-9), alphabets (A-Z and a-z), and other special characters.It must contain '@'.It should not contain whitespace.It may or may not contain a dot (.) or hyphen (-). UPI st
6 min read
Validate Gender using Regular Expressions
Given some words of Gender, the task is to check if they are valid or not using regular expressions. The correct responses can be as given below: Male / male / MALE / M / mFemale / female / FEMALE / F / fNot prefer to say Example: Input: MOutput: True Input: SOutput: False Approach: The problem can be solved based on the following idea: Create a re
6 min read
Validating Programming File formats using Regular Expressions
Given some programming file names, the task is to check if they are valid or not using regular expressions. A valid programming file name follows the below conditions: It can contain alphabets (UpperCase or LowerCase), and digits and may contain a hyphen.It should contain one dot only that will be used as a suffix to save the file.Avoid special cha
6 min read
Regular Expressions to Validate ISBN Code
Given some ISBN Codes, the task is to check if they are valid or not using regular expressions. Rules for the valid codes are: It is a unique 10 or 13-digit.It may or may not contain a hyphen.It should not contain whitespaces and other special characters.It does not allow alphabet letters. Examples: Input: str = ”978-1-45678-123-4?Output: True Inpu
5 min read
Pension Scheme Tax Reference Number Validation using Regular Expressions
Given some Pension Scheme Tax Reference Numbers, the task is to check if they are valid or not using regular expressions. Rules for the valid Tax Reference Numbers are : It is an alphanumeric string ie., digits(0-9) and Uppercase Alphabet letters(A-Z).It does not contain whitespaces and other special characters.It starts with a digit and ends wends
6 min read
Extracting all present dates in any given String using Regular Expressions
Given a string Str, the task is to extract all the present dates from the string. Dates can be in the format i.e., mentioned below: DD-MM-YYYYYYYY-MM-DDDD Month YYYY Examples: Input: Str = "The First Version was released on 12-07-2008.The next Release will come on 12 July 2009. The due date for payment is 2023-09-1. India gained its freedom on 15 A
6 min read
Article Tags :
Practice Tags :