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 idea is to use Regular Expression to solve this problem.
 Create a regular expression to find currency symbol in the string as mentioned below :
regex = “\\p{Sc}”;
Where:
{\\p{Sc} represents any currency sign.  Match the given string with the Regular Expression using Pattern.matcher().
 Print index of the character of the string for which a match is found with the given regular expression.
 Find the character in first string that is present at minimum index in second string
 Parsing String of symbols to Expression
 Find last index of a character in a string
 Split numeric, alphabetic and special symbols from a String
 Find i'th Index character in a binary string obtained after n iterations
 Minimum index i such that all the elements from index i to given index are equal
 Check if a String contains any index with more than K active characters
 Minimum cost to traverse from one index to another in the String
 Index of character depending on frequency count in string
 Largest index for each distinct character in given string with frequency K
 WildCard pattern matching having three symbols ( * , + , ? )
 Find the MidAlphabet for each index of the given Pair of Strings
 Find the index of an array element in Java
 Find the element at the specified index of a Spirally Filled Matrix
 Find two equal subsequences of maximum length with at least one different index
 Find index of first occurrence when an unsorted array is sorted
 Find a Fixed Point (Value equal to index) in a given array
 Find index of an extra element present in one sorted array
 Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated
 Find initial sequence that produces a given Array by cyclic increments upto index P

The above approach requires Auxiliary Space for storing all the currencies in the set.
Efficient Approach:
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); } } 
$  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 studentfriendly price and become industry ready.
Recommended Posts:
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.