Friday, February 24, 2023
HomeSoftware DevelopmentConvert String to foreign money format

# Convert String to foreign money format

Given a quantity N. Convert N into an Indian foreign money 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 examine 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.
• 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.

Under is the implementation of the code:

## C++

 `#embrace ` `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;` `        ``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