Related Articles
How to find index of any Currency Symbols in a given string
• Last Updated : 16 Feb, 2021

Given a string txt, the task is to find the index of currency symbols present in the given string.
Examples:

Input: txt = “Currency symbol of USA is \$”;
Output: 26
Explanation :
The symbol \$ is present at index 33.
Input: txt = “One US Dollar(\$) is equal to 75.70 Indian Rupee.”;
Output: 14

Naive Approach:
The simplest approach to solve the problem is to do the following:

• Create a set of all currencies.
• Traverse the string and if any of the currency symbols present in the set is found in the string, print it’s index.

The above approach requires Auxiliary Space for storing all the currencies in the set.

Efficient Approach:

1. The idea is to use Regular Expression to solve this problem.
2. Create a regular expression to find currency symbol in the string as mentioned below :
regex = “\\p{Sc}“;
Where:
{\\p{Sc}
represents any currency sign.
For C++ / Python, we can use regex = “\\\$|\\£|\\€
Where the regex checks if any of the given currency symbol ( \$, £, € ) is present in the string.
3. Match the given string with the Regular Expression using Pattern.matcher().
4. Print index of the character of the string for which a match is found with the given regular expression.

Below is the implementation of the above approach:

## C++

 `// C++ program to find indices of``// currency symbols present in a``// string using regular expression``#include ``#include ``using` `namespace` `std;` `// Function to find currency symbol``// in a text using regular expression``void` `findCurrencySymbol(string text)``{` `  ``// Regex to find any currency``  ``// symbol in a text``  ``const` `regex pattern(``"\\\$|\\£|\\€"``);``  ``for` `(``auto` `it = sregex_iterator(text.begin(), text.end(), pattern);``       ``it != sregex_iterator(); it++)``  ``{``    ` `      ``// flag type for determining the matching behavior``      ``// here it is for matches on 'string' objects``      ``smatch match;``      ``match = *it;``      ``cout << match.str(0) << ``" - "` `<< match.position(0) << endl;``  ``}``  ``return` `;``}` `// Driver Code``int` `main()``{``  ``string txt``      ``= ``"\$27 - \$21.30equal to \$5.70"``;``  ``findCurrencySymbol(txt);``  ``return` `0;``}` `// This code is contributed by yuvraj_chandra`

## Java

 `// Java program to find indices of``// currency symbols present in a``// string using regular expression``import` `java.util.regex.*;``class` `GFG {` `    ``// Function to find currency symbol``    ``// in a text using regular expression``    ``public` `static` `void` `findCurrencySymbol(``        ``String text)``    ``{` `        ``// Regex to find any currency``        ``// symbol in a text``        ``String regex = ``"\\p{Sc}"``;` `        ``// Compile the ReGex``        ``Pattern p = Pattern.compile(``            ``regex);` `        ``// Find match between the``        ``// given string and the``        ``// Regex using Pattern.matcher()``        ``Matcher m = p.matcher(text);` `        ``// Find the next subsequence``        ``// of the input subsequence``        ``// that matches the pattern``        ``while` `(m.find()) {` `            ``System.out.println(``                ``text.charAt(m.start())``                ``+ ``" - "``                ``+ m.start());``        ``}``    ``}``    ``// Driver Code``    ``public` `static` `void` `main(String args[])``    ``{``        ``String txt = ``"\$27 - \$21.30"``                    ``+ ``"equal to \$5.70"``;``        ``findCurrencySymbol(txt);``    ``}``}`

## Python3

 `# Python program to find indices of``# currency symbols present in a``# string using regular expression``import` `re` `# Function to find currency symbol``# in a text using regular expression``def` `findCurrencySymbol(text):` `    ``# Regex to find any currency``    ``# symbol in a text``    ``regex ``=` `"\\\$|\\£|\\€"` `    ``for` `m ``in` `re.finditer(regex, text):``        ``print``(text[m.start(``0``)], ``"-"` `,m.start(``0``))`  `# Driver code``txt ``=` `"\$27 - \$21.30equal to \$5.70"``findCurrencySymbol(txt)` `# This code is contributed by yuvraj_chandra`

Output:

```\$ - 0
\$ - 6
\$ - 21```

Time Complexity: O(N)

Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up