{ }codelper
replacing textP0412

A price tag with dots and commas swapped

Some countries write numbers with a comma (,) as the decimal mark and a dot (.) to separate thousands.
For example, 1.234.567,89 there is the number written 1,234,567.89 here.
You want to rewrite one line of such a price tag in our style.
Print the line with every dot (.) changed to a comma (,) and every comma (,) changed to a dot (.).
Both changes happen at the same time.
A comma that came from a dot is not changed back to a dot, and a dot that came from a comma is not changed back to a comma.
Dots and commas outside numbers (a dot ending a sentence, a comma after a word) are changed the same way.
Characters other than dots and commas stay as they are.
If there is no dot or comma, print the line unchanged.

Input
One line of text, length 1 to 200.
It consists only of English letters (either case), digits, spaces, dots (.), commas (,) and colons (:), and its first and last characters are not spaces.
Output
one line, the text with dots and commas swapped
Example 1 · stdin
Preis: 1.299,00 EUR
stdout
Preis: 1,299.00 EUR
Example 2 · stdin
Total: 12.345.678,90
stdout
Total: 12,345,678.90
main.py
Input (stdin)

When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).

$ Ready

Open this on a computer to write and run code. On mobile you can read the problem and the solutions.

In other languagesShow solutions by language · 8 languages

This task needs both replacements to happen at once.
Python swaps the pair of characters in one go with s.translate(str.maketrans(".,", ",.")).
Java s.replace('.', '#'), C# s.Replace('.', '#'), JS/TS s.replaceAll(".", "#") and Craykon s.Replace(".", "#") all replace every match literally.
So #, which never appears in the input, is used as a temporary spot: replace .→#, then ,→., then #→, (if # could appear in the input, this would be wrong).
C has no string replace function, so it walks the characters and swaps with if … else if.
C++ has std::replace(s.begin(), s.end(), '.', '#') in <algorithm>, so the same # method would work there too, but this solution walks the characters like C.
Trap one: replacing twice without a temporary spot, as in s.replace(".", ",").replace(",", "."), also turns the commas made by the first step into dots, so 1.299,00 becomes 1.299.00.
Trap two: JS/TS s.replace(".", "#") with a string pattern replaces only the first match, so 12.345.678,90 becomes 12,345.678.90.
Trap three: Java s.replaceAll(".", "#"), despite its name, reads its first argument as a regex, so . matches any character.
The whole line becomes #, and the last step turns the whole line into commas.
Trap four: in C, writing if (s[i] == '.') s[i] = ','; and then if (s[i] == ',') s[i] = '.'; without else turns the comma just made straight back into a dot, giving the same answer as trap one.

Pythonmain.py · 3 lines
s = input()# 점과 쉼표를 한꺼번에 맞바꾼다print(s.translate(str.maketrans(".,", ",.")))
JavaScriptmain.js · 4 lines
const s = require("fs").readFileSync(0, "utf8").split("\n")[0].replace(/\r$/, "");// 입력에 없는 # 를 잠깐 맡겨 두는 자리로 쓴다const out = s.replaceAll(".", "#").replaceAll(",", ".").replaceAll("#", ",");console.log(out);
TypeScriptmain.ts · 5 lines
import { readFileSync } from "node:fs";const s: string = readFileSync(0, "utf8").split("\n")[0].replace(/\r$/, "");// 입력에 없는 # 를 잠깐 맡겨 두는 자리로 쓴다const out: string = s.replaceAll(".", "#").replaceAll(",", ".").replaceAll("#", ",");console.log(out);
JavaMain.java · 12 lines
import java.io.BufferedReader;import java.io.InputStreamReader; public class Main {    public static void main(String[] args) throws Exception {        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));        String s = br.readLine();        // replace는 글자 그대로 모두 바꾼다. 입력에 없는 #를 잠깐 맡겨 두는 자리로 쓴다        String out = s.replace('.', '#').replace(',', '.').replace('#', ',');        System.out.println(out);    }}
C#Main.cs · 12 lines
using System; class Program{    static void Main()    {        string s = Console.ReadLine();        // Replace(char, char)는 모두 바꾼다. 입력에 없는 #를 잠깐 맡겨 두는 자리로 쓴다        string output = s.Replace('.', '#').Replace(',', '.').Replace('#', ',');        Console.WriteLine(output);    }}
C++main.cpp · 13 lines
#include <iostream>#include <string> int main() {    std::string s;    std::getline(std::cin, s);    if (!s.empty() && s.back() == '\r') s.pop_back();    for (char& c : s) {        if (c == '.') c = ',';        else if (c == ',') c = '.';    }    std::cout << s << "\n";}
Cmain.c · 14 lines
#include <stdio.h>#include <string.h> int main(void) {    char s[256];    if (!fgets(s, sizeof s, stdin)) return 0;    s[strcspn(s, "\r\n")] = '\0';    for (int i = 0; s[i]; i++) {        if (s[i] == '.') s[i] = ',';        else if (s[i] == ',') s[i] = '.';    }    puts(s);    return 0;}
Craykonmain.cray · 6 lines
s = Console.ReadLine();// 입력에 없는 # 를 잠깐 맡겨 두는 자리로 쓴다s = s.Replace(".", "#");s = s.Replace(",", ".");s = s.Replace("#", ",");Console.WriteLine(s);