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

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:
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.For C++ / Python, we can use regex = “\\$|\\£|\\€“Where the regex checks if any of the given currency symbol ( $, £, € ) is present in the string.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.
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.For C++ / Python, we can use regex = “\\$|\\£|\\€“Where the regex checks if any of the given currency symbol ( $, £, € ) is present in the string.
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.
Below is the implementation of the above approach:
C++
Java
Python3
// C++ program to find indices of// currency symbols present in a// string using regular expression#include <iostream>#include <regex>using namespace std; // Function to find currency symbol// in a text using regular expressionvoid 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 Codeint main(){  string txt      = "$27 - $21.30equal to $5.70";  findCurrencySymbol(txt);  return 0;} // This code is contributed by yuvraj_chandra
// Java program to find indices of// currency symbols present in a// string using regular expressionimport 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);    }}
# Python program to find indices of# currency symbols present in a# string using regular expressionimport re # Function to find currency symbol# in a text using regular expressiondef 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 codetxt = "$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)
yuvraj_chandra
CPP-regex
java-regular-expression
regular-expression
Pattern Searching
Searching
Strings
Searching
Strings
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Reverse the substrings of the given String according to the given Array of indices
Check if the given string is shuffled substring of another string
How to validate GUID (Globally Unique Identifier) using Regular Expression
How to check Aadhaar number is valid or not using Regular Expression
How to validate pin code of India using Regular Expression
Binary Search
Maximum and minimum of an array using minimum number of comparisons
Linear Search
K'th Smallest/Largest Element in Unsorted Array | Set 1
Search an element in a sorted and rotated array