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:
- Remove all the separators (, ) from the string.
- Iterate from the end of the string and place a separator(, ) after every third number.
- Print the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string convert(string input)
{
int len = input.length();
for ( int i = 0; i < len; i++) {
if (input[i] == ',' ) {
input.erase(input.begin() + i);
len--;
i--;
}
}
string output = "" ;
int ctr = 0;
for ( int i = len - 1; i >= 0; i--) {
ctr++;
output = input[i] + output;
if (ctr % 3 == 0 && ctr < len) {
output = ',' + output;
}
}
return output;
}
int main()
{
string input1 = "12,34,56,789" ;
string input2 = "90,05,00,00,000" ;
cout << convert(input1) << endl;
cout << convert(input2) << endl;
}
|
Java
public class IndianToInternationalConversion {
static String convert(String input) {
input = input.replaceAll( "," , "" );
StringBuilder output = new StringBuilder();
int ctr = 0 ;
for ( int i = input.length() - 1 ; i >= 0 ; i--) {
ctr++;
output.insert( 0 , input.charAt(i));
if (ctr % 3 == 0 && ctr < input.length()) {
output.insert( 0 , ',' );
}
}
return output.toString();
}
public static void main(String[] args) {
String input1 = "12,34,56,789" ;
String input2 = "90,05,00,00,000" ;
System.out.println(convert(input1));
System.out.println(convert(input2));
}
}
|
Python
def convert(input_str):
len_input = len (input_str)
i = 0
while i < len_input:
if input_str[i] = = ',' :
input_str = input_str[:i] + input_str[i + 1 :]
len_input - = 1
else :
i + = 1
output = ""
ctr = 0
for i in range (len_input - 1 , - 1 , - 1 ):
ctr + = 1
output = input_str[i] + output
if ctr % 3 = = 0 and ctr < len_input:
output = ',' + output
return output
if __name__ = = "__main__" :
input1 = "12,34,56,789"
input2 = "90,05,00,00,000"
print (convert(input1))
print (convert(input2))
|
C#
using System;
using System.Text;
class GFG
{
static string Convert( string input)
{
input = input.Replace( "," , "" );
StringBuilder output = new StringBuilder();
int ctr = 0;
for ( int i = input.Length - 1; i >= 0; i--)
{
ctr++;
output.Insert(0, input[i]);
if (ctr % 3 == 0 && ctr < input.Length)
{
output.Insert(0, ',' );
}
}
return output.ToString();
}
public static void Main( string [] args)
{
string input1 = "12,34,56,789" ;
string input2 = "90,05,00,00,000" ;
Console.WriteLine(Convert(input1));
Console.WriteLine(Convert(input2));
}
}
|
Javascript
function convert(input) {
input = input.replace(/,/g, '' );
let output = '' ;
let ctr = 0;
for (let i = input.length - 1; i >= 0; i--) {
ctr++;
output = input.charAt(i) + output;
if (ctr % 3 === 0 && ctr < input.length) {
output = ',' + output;
}
}
return output;
}
const input1 = '12,34,56,789' ;
const input2 = '90,05,00,00,000' ;
console.log(convert(input1));
console.log(convert(input2));
|
Output
123,456,789
90,050,000,000
Related article:
Convert the number from International system to Indian system
Last Updated :
07 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...