HomeSoftware DevelopmentConvert String to forex format

Convert String to forex format


Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Given a quantity N. Convert N into an Indian forex format. For extra understanding, please look into examples.

Examples:

Enter: N = 1000000
Output: Rs 10, 00, 000

Enter: N = 1500
Output: Rs 1, 500

Strategy: Steps concerned within the implementation of code:

  • We have to test whether or not the size of the string is even or odd.
  • If the size of the string is lower than equal to three we’ll merely return it else we’ll do the next, newString = “”.
  • if the size is odd:
    • we’ll the primary character into a brand new string newSstring = N[0].
    • after which we’ll and commas new string = “, “.
  • Now we’ll skip the 2 characters after which add “, ” until the size < n-2.
  • And ultimately, we’ll add the remaining characters to the newString.

Beneath is the implementation of the code:

C++

#embody <bits/stdc++.h>

utilizing namespace std;

  

string goodFormat(string s, int n)

{

  

    

    if (n <= 3)

        return "Rs. " + s;

  

    string ans = "";

    int begin = 0, cnt = 0;

  

    

    if (n % 2 == 0) {

        ans += s[0];

        ans += ", ";

        begin = 1;

    }

    whereas (begin < n - 2) {

        if (cnt == 2) {

            ans += ", ";

            cnt = 0;

            proceed;

        }

        else {

            ans += s[start];

            cnt++;

        }

        begin++;

    }

    for (int i = begin; i < n; i++)

        ans += s[i];

  

    return "Rs " + ans;

}

  

int fundamental()

{

    string s = "1000000";

    int l = s.size();

  

    

    cout << goodFormat(s, l);

    return 0;

}

Time Complexity: O(N)
Auxiliary Area: O(1)

RELATED ARTICLES

Most Popular

Recent Comments