Convert the number from Indian system to International system

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.