VOOZH about

URL: https://www.geeksforgeeks.org/dsa/how-to-find-index-of-any-currency-symbols-in-a-given-string/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 15 Jul, 2025

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:

Output:

$ - 0
$ - 6
$ - 21

Time Complexity: O(N)

Auxiliary Space: O(1)

Comment