Given string str which represents a number with separators(,) in the International number system, the task is to convert this string representation into the Indian Numeric System.
Examples:
Input: str = “123,456,789”
Output: 12,34,56,789
Explanation:
The given string represents a number in the international system. It is converted to the Indian system.
Input: str = “90,050,000,000”
Output: 90,05,00,00,000
International Numeric System: The International numbering system is used outside the Indian subcontinent to express large numbers. It follows the following schema:
Number | Applying separators | In words |
---|---|---|
1 | 1 | One |
10 | 10 | Ten |
100 | 100 | One hundred |
1000 | 1,000 | One thousand |
10000 | 10,000 | Ten thousand |
100000 | 100,000 | One hundred thousand |
1000000 | 1,000,000 | One million |
10000000 | 10,000,000 | Ten million |
100000000 | 100,000,000 | One hundred million |
1000000000 | 1,000,000,000 | One billion |
Indian Numeric System: The Indian numbering system is used in the Indian subcontinent to express large numbers. It follows the following schema:
Number | Applying separators | In words |
---|---|---|
1 | 1 | One |
10 | 10 | Ten |
100 | 100 | One hundred |
1000 | 1,000 | One thousand |
10000 | 10,000 | Ten thousand |
100000 | 1,00,000 | One lakh |
1000000 | 10,00,000 | Ten lakh |
10000000 | 1,00,00,000 | One crore |
100000000 | 10,00,00,000 | Ten crore |
1000000000 | 100,00,00,000 | One hundred crore |
Approach: From the above representations, the idea is to first obtain the number without any separators. Therefore:
- Remove all the separators(, ) from the string.
- Process the string.
- Now put a separator(, ) after the third number and then after every second number.
- Reverse the string (This converts the number into the Indian Numeric System) and print it.
Below is the implementation of the above approach:
// C++ program to convert the number // from International system // to Indian system #include <bits/stdc++.h> using namespace std;
// Function to convert a number represented // in International numeric system to // Indian numeric system. string convert(string input) { // Find the length of the
// input string
int len = input.length();
// Removing all the separators(, )
// from the input string
for ( int i = 0; i < len;) {
if (input[i] == ',' ) {
input.erase(input.begin() + i);
len--;
i--;
}
else {
i++;
}
}
// Reverse the input string
reverse(input.begin(), input.end());
// Declaring the output string
string output;
// Process the input string
for ( int i = 0; i < len; i++) {
// Add a separator(,) after the
// third number
if (i == 2) {
output += input[i];
output += "," ;
}
// Then add a separator(,) after
// every second number
else if (i > 2 && i % 2 == 0
&& i + 1 < len) {
output += input[i];
output += "," ;
}
else {
output += input[i];
}
}
// Reverse the output string
reverse(output.begin(), output.end());
// Return the output string back
// to the main function
return output;
} // Driver code int main()
{ string input1 = "123,456,789" ;
string input2 = "90,050,000,000" ;
cout << convert(input1) << endl;
cout << convert(input2);
} |
// Java program to convert the number // from International system // to Indian system import java.util.*;
class GFG{
// Function to convert a number represented // in International numeric system to // Indian numeric system. static String convert(String input)
{ StringBuilder sbInput = new StringBuilder(input);
// Find the length of the
// sbInput
int len = sbInput.length();
// Removing all the separators(, )
// from the sbInput
for ( int i = 0 ; i < len;)
{
if (sbInput.charAt(i) == ',' )
{
sbInput.deleteCharAt(i);
len--;
i--;
}
else
{
i++;
}
}
// Reverse the sbInput
StringBuilder sbInputReverse = sbInput.reverse();
// Declaring the output
StringBuilder output = new StringBuilder();
// Process the sbInput
for ( int i = 0 ; i < len; i++)
{
// Add a separator(, ) after the
// third number
if (i == 2 )
{
output.append(sbInputReverse.charAt(i));
output.append( "," );
}
// Then add a separator(, ) after
// every second number
else if (i > 2 && i % 2 == 0 && i + 1 < len)
{
output.append(sbInputReverse.charAt(i));
output.append( "," );
}
else
{
output.append(sbInputReverse.charAt(i));
}
}
// Reverse the output
StringBuilder reverseOutput = output.reverse();
// Return the output string back
// to the main function
return reverseOutput.toString();
} // Driver code public static void main(String[] args)
{ String input1 = "123,456,789" ;
String input2 = "90,050,000,000" ;
System.out.println(convert(input1));
System.out.println(convert(input2));
} } // This code is contributed by offbeat |
# Python3 program to convert # the number from International # system to Indian system # Function to convert a number # represented in International # numeric system to Indian numeric # system. def convert( input ):
# Find the length of the
# input string
Len = len ( input )
# Removing all the separators(, )
# from the input string
i = 0
while (i < Len ):
if ( input [i] = = "," ):
input = input [:i] + input [i + 1 :]
Len - = 1
i - = 1
else :
i + = 1
# Reverse the input string
input = input [:: - 1 ]
# Declaring the output string
output = ""
# Process the input string
for i in range ( Len ):
# Add a separator(, ) after the
# third number
if (i = = 2 ):
output + = input [i]
output + = ","
# Then add a separator(, ) after
# every second number
elif (i > 2 and i % 2 = = 0 and
i + 1 < Len ):
output + = input [i]
output + = ","
else :
output + = input [i]
# Reverse the output string
output = output[:: - 1 ]
# Return the output string back
# to the main function
return output
# Driver code input1 = "123,456,789"
input2 = "90,050,000,000"
print (convert(input1))
print (convert(input2))
# This code is contributed by avanitrachhadiya2155 |
// C# program to convert the number // from International system // to Indian system using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
class GFG {
// Function to convert a number represented
// in International numeric system to
// Indian numeric system.
static string convert( string input)
{
StringBuilder sbInput = new StringBuilder(input);
// Find the length of the
// sbInput
int len = sbInput.Length;
// Removing all the separators(, )
// from the sbInput
for ( int i = 0; i < len;) {
if (sbInput[i] == ',' ) {
sbInput.Remove(i, 1);
len--;
i--;
}
else {
i++;
}
}
// Reverse the sbInput
StringBuilder sbInputReverse = new StringBuilder();
string st1 = new string (
sbInput.ToString().Reverse().ToArray());
sbInputReverse.Append(st1);
// Declaring the output
StringBuilder output = new StringBuilder();
// Process the sbInput
for ( int i = 0; i < len; i++) {
// Add a separator(,) after the
// third number
if (i == 2) {
output.Append(sbInputReverse[i]);
output.Append( "," );
}
// Then add a separator(,) after
// every second number
else if (i > 2 && i % 2 == 0 && i + 1 < len) {
output.Append(sbInputReverse[i]);
output.Append( "," );
}
else {
output.Append(sbInputReverse[i]);
}
}
// Reverse the output
StringBuilder reverseOutput = new StringBuilder();
st1 = new string (
output.ToString().Reverse().ToArray());
reverseOutput.Append(st1);
// Return the output string back
// to the main function
return reverseOutput.ToString();
}
// Driver code
public static void Main( string [] args)
{
string input1 = "123,456,789" ;
string input2 = "90,050,000,000" ;
Console.WriteLine(convert(input1));
Console.WriteLine(convert(input2));
}
} // This code is contributed by phasing17 |
/* JavaScript program to convert the number from International Numeric System to Indian Numeric System */ /* Function to convert a number represented in International Numeric System to Indian Numeric System */ function convert(input) {
// Remove all the separators (, ) from the inputString
const str = input.replace(/[\s,]+/g, '' );
// Declaring empty array
const arr = [];
let i = str.length - 1;
/* Traversing through the characters of `str`
and pushing each character to the array */
for (let j = str.length - 1; j > i; j--)
arr.push(str[j]);
// Traversing through the characters of `str`
for (let j = 0; i >= 0; i--, j++) {
/* adding comma separator after every
second character (starting from the third) */
if (j > 2 && j % 2 == 1) arr.push( ',' );
// Pushing each character to the array
arr.push(str[i]);
};
// Reverse the array, join it and return
return arr.reverse().join( '' );
}; // Driver code const input1 = '123,456,789' ;
const input2 = '90,050,000,000' ;
const input3 = '48,605,000,000' ;
// Write the converted numbers console.log(convert(input1)); console.log(convert(input2)); console.log(convert(input3)); // This code is contributed by shinjanpatra // This code is improved by Vaibhav Naik |
12,34,56,789 90,05,00,00,000
Time Complexity: O(n2), where n is the length of the string
Auxiliary Space: O(1)
Convert the number to Indian System in linear time O(N)
In above solution we had used the .erase() in C++ and .deleteCharAt() in Java, but instead of using that function we can do the same using for loop. So in this method we will remove ‘,’ and ‘ ‘ (extra space) by single for loop so our time complexity will be reduced from O(n2) to O(n).
// C++ program to convert the number from International system to Indian system #include <bits/stdc++.h> using namespace std;
// Function to convert a number represented in International numeric system to Indian numeric system. string convert(string input) { // Find the length of the input string
int len = input.length();
// string which store only digit presemt in input string
string onlyDigits = "" ;
// store only digit present in input to string onlyDigit
for ( int i = 0; i < len; i++) {
if (input[i] != ',' ) {
onlyDigits += input[i];
}
}
// update the length of string
len = onlyDigits.length();
// Reverse the onlyDigit string
reverse(onlyDigits.begin(), onlyDigits.end());
// Declaring the output string
string output = "" ;
// Process the input string
for ( int i = 0; i < len; i++) {
// Add a separator(,) after the third number
if (i == 2) {
output += onlyDigits[i];
output += "," ;
}
// Then add a separator(,) after every second number
else if (i > 2 && i % 2 == 0 && i + 1 < len) {
output += onlyDigits[i];
output += "," ;
}
else {
output += onlyDigits[i];
}
}
// Reverse the output string
reverse(output.begin(), output.end());
// Return the output string back to the main function
return output;
} // Driver code int main()
{ string input1 = "123,456,789" ;
string input2 = "90,050,000,000" ;
cout << convert(input1) << endl;
cout << convert(input2);
} |
import java.util.Scanner;
public class IndianNumberConversion {
// Function to convert a number represented in International numeric system to Indian numeric system.
static String convert(String input) {
// Find the length of the input string
int len = input.length();
// string which stores only digits present in the input string
StringBuilder onlyDigits = new StringBuilder();
// store only digits present in the input to the StringBuilder onlyDigits
for ( int i = 0 ; i < len; i++) {
if (input.charAt(i) != ',' ) {
onlyDigits.append(input.charAt(i));
}
}
// update the length of the string
len = onlyDigits.length();
// Reverse the onlyDigits string
onlyDigits.reverse();
// Declaring the output string
StringBuilder output = new StringBuilder();
// Process the input string
for ( int i = 0 ; i < len; i++) {
// Add a separator(,) after the third number
if (i == 2 ) {
output.append(onlyDigits.charAt(i)).append( "," );
}
// Then add a separator(,) after every second number
else if (i > 2 && i % 2 == 0 && i + 1 < len) {
output.append(onlyDigits.charAt(i)).append( "," );
} else {
output.append(onlyDigits.charAt(i));
}
}
// Reverse the output string
output.reverse();
// Return the output string back to the main function
return output.toString();
}
// Driver code
public static void main(String[] args) {
String input1 = "123,456,789" ;
String input2 = "90,050,000,000" ;
System.out.println(convert(input1));
System.out.println(convert(input2));
}
} |
def convert(input_str):
# Find the length of the input string
length = len (input_str)
# String to store only digits present in the input string
only_digits = ""
# Store only digits present in the input to the only_digits string
for i in range (length):
if input_str[i] ! = ',' :
only_digits + = input_str[i]
# Update the length of the string
length = len (only_digits)
# Reverse the only_digits string
only_digits = only_digits[:: - 1 ]
# Declare the output string
output = ""
# Process the input string
for i in range (length):
# Add a separator (,) after the third number
if i = = 2 :
output + = only_digits[i] + ","
# Then add a separator (,) after every second number
elif i > 2 and i % 2 = = 0 and i + 1 < length:
output + = only_digits[i] + ","
else :
output + = only_digits[i]
# Reverse the output string
output = output[:: - 1 ]
# Return the output string back to the calling function
return output
# Driver code if __name__ = = "__main__" :
input1 = "123,456,789"
input2 = "90,050,000,000"
print (convert(input1))
print (convert(input2))
|
using System;
class Program
{ // Function to convert the input string based on specific rules
static string Convert( string inputStr)
{
// Find the length of the input string
int length = inputStr.Length;
string onlyDigits = "" ;
// Extract digits from the input string
for ( int i = 0; i < length; i++)
{
if (inputStr[i] != ',' )
{
onlyDigits += inputStr[i];
}
}
// Update the length after removing commas
length = onlyDigits.Length;
// Reverse the string containing only digits
char [] charArray = onlyDigits.ToCharArray();
Array.Reverse(charArray);
onlyDigits = new string (charArray);
string output = "" ;
// Process the string to apply formatting rules
for ( int i = 0; i < length; i++)
{
// Add a separator (,) after the third number
if (i == 2)
{
output += onlyDigits[i] + "," ;
}
// Add a separator (,) after every second number beyond the third
else if (i > 2 && i % 2 == 0 && i + 1 < length)
{
output += onlyDigits[i] + "," ;
}
else
{
output += onlyDigits[i];
}
}
// Reverse the output string
charArray = output.ToCharArray();
Array.Reverse(charArray);
output = new string (charArray);
return output;
}
// Entry point of the program
static void Main( string [] args)
{
// Input strings
string input1 = "123,456,789" ;
string input2 = "90,050,000,000" ;
// Output converted strings
Console.WriteLine(Convert(input1));
Console.WriteLine(Convert(input2));
}
} |
function convert(input) {
// Find the length of the input string
let len = input.length;
// String to store only digits present in input
let onlyDigits = "" ;
// Store only digits present in input to onlyDigits string
for (let i = 0; i < len; i++) {
if (input[i] !== ',' ) {
onlyDigits += input[i];
}
}
// Update the length of the string
len = onlyDigits.length;
// Reverse the onlyDigits string
onlyDigits = onlyDigits.split( '' ).reverse().join( '' );
// Initialize the output string
let output = "" ;
// Process the input string
for (let i = 0; i < len; i++) {
// Add a separator(,) after the third number
if (i === 2) {
output += onlyDigits[i] + "," ;
}
// Then add a separator(,) after every second number
else if (i > 2 && i % 2 === 0 && i + 1 < len) {
output += onlyDigits[i] + "," ;
} else {
output += onlyDigits[i];
}
}
// Reverse the output string
output = output.split( '' ).reverse().join( '' );
// Return the output string
return output;
} // Driver code let input1 = "123,456,789" ;
let input2 = "90,050,000,000" ;
console.log(convert(input1)); // Output for input1
console.log(convert(input2)); // Output for input2
|
12,34,56,789 90,05,00,00,000
Time Complexity: O(n), where n is the length of string
Auxiliary Space: O(1)