# How to find index of any Currency Symbols in a given string

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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.
1. 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.

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:

## 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); ` `    ``} ` `} `

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 If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.