Skip to content
Related Articles
Open in App
Not now

Related Articles

Convert the number from Indian system to International system

Improve Article
Save Article
  • Last Updated : 04 Jun, 2020
Improve Article
Save Article

Given an input string N consisting of numerals and separators (, ) in the Indian Numeric System, the task is to print the string after placing separators(, ) based on International Numeric System.

Examples:

Input: N = “12, 34, 56, 789”
Output: 123, 456, 789

Input: N = “90, 05, 00, 00, 000”
Output: 90, 050, 000, 000

Approach:

  1. Remove all the separators (, ) from the string.
  2. Iterate from the end of the string and place a separator(, ) after every third number.
  3. Print the result.

Below is the implementation of the above approach:

C++




// C++ Program to convert
// the number from Indian system
// to International system
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to convert Indian Numeric
// System to International Numeric System
string convert(string input)
{
    // Length of the input string
    int len = input.length();
  
    // Removing all the separators(, )
    // From the input string
    for (int i = 0; i < len; i++) {
        if (input[i] == ',') {
            input.erase(input.begin() + i);
            len--;
            i--;
        }
    }
  
    // Initialize output string
    string output = "";
    int ctr = 0;
  
    // Process the input string
    for (int i = len - 1; i >= 0; i--) {
  
        ctr++;
        output = input[i] + output;
  
        // Add a separator(, ) after
        // every third digit
        if (ctr % 3 == 0 && ctr < len) {
            output = ',' + output;
        }
    }
  
    // Return the output string back
    // to the main function
    return output;
}
  
// Driver Code
int main()
{
    string input1 = "12,34,56,789";
    string input2 = "90,05,00,00,000";
  
    cout << convert(input1) << endl;
    cout << convert(input2) << endl;
}

Output:

123,456,789
90,050,000,000

Related article: Convert the number from International system to Indian system


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!