How to find index of any Currency Symbols in a given string
Last Updated :
11 Apr, 2023
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.
Below is the implementation of the above approach:
C++
#include <iostream>
#include <regex>
using namespace std;
void findCurrencySymbol(string text)
{
const regex pattern( "\\$|\\£|\\€" );
for ( auto it = sregex_iterator(text.begin(), text.end(), pattern);
it != sregex_iterator(); it++)
{
smatch match;
match = *it;
cout << match.str(0) << " - " << match.position(0) << endl;
}
return ;
}
int main()
{
string txt
= "$27 - $21.30equal to $5.70" ;
findCurrencySymbol(txt);
return 0;
}
|
Java
import java.util.regex.*;
class GFG {
public static void findCurrencySymbol(
String text)
{
String regex = "\\p{Sc}" ;
Pattern p = Pattern.compile(
regex);
Matcher m = p.matcher(text);
while (m.find()) {
System.out.println(
text.charAt(m.start())
+ " - "
+ m.start());
}
}
public static void main(String args[])
{
String txt = "$27 - $21.30"
+ "equal to $5.70" ;
findCurrencySymbol(txt);
}
}
|
C#
using System;
using System.Text.RegularExpressions;
class GFG {
public static void findCurrencySymbol(
string text)
{
string regex = "\\p{Sc}" ;
Regex p = new Regex(regex);
Match m = p.Match(text);
while (m.Success) {
Console.WriteLine(
text[m.Index]
+ " - "
+ m.Index);
m = m.NextMatch();
}
}
public static void Main()
{
string txt = "$27 - $21.30"
+ "equal to $5.70" ;
findCurrencySymbol(txt);
}
}
|
Python3
import re
def findCurrencySymbol(text):
regex = "\\$|\\£|\\€"
for m in re.finditer(regex, text):
print (text[m.start( 0 )], "-" ,m.start( 0 ))
txt = "$27 - $21.30equal to $5.70"
findCurrencySymbol(txt)
|
PHP
<?php
function findCurrencySymbol( $text ) {
$regex = '/\p{Sc}/u' ;
$matches = [];
preg_match_all( $regex , $text , $matches , PREG_OFFSET_CAPTURE);
foreach ( $matches [0] as $match ) {
echo $match [0] . ' - ' . $match [1] . "\n" ;
}
}
$txt = '$27 - $21.30equal to $5.70' ;
findCurrencySymbol( $txt );
|
Javascript
function findCurrencySymbol(text) {
const regex = /\p{Sc}/gu;
let match;
while ((match = regex.exec(text)) !== null ) {
console.log(`${match[0]} - ${match.index}`);
}
}
const txt = "$27 - $21.30equal to $5.70" ;
findCurrencySymbol(txt);
|
Output:
$ - 0
$ - 6
$ - 21
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...