Read the number as a string and the digits and as characters. Replace all occurrences of character in the string with character . Then print the resulting string, removing any leading zeros. If the final string consists only of zeros, print a single zero.
The resulting number may exceed the range of the int type. For example, if n = 1000000000~(10^9), a = 1, b = 9, the result will be 9000000000~(9 \cdot 10^9).
Algorithm implementation
Store the number as a string in the array .
char s[100];Read the number as a string and the digits and as characters.
scanf("%s %c %c",s,&a,&b);Replace all occurrences of character with character in the string .
for (i = 0; i < strlen(s); i++)
if (s[i] == a) s[i] = b;Convert the string into a number of type long long.
res = atoll(s);Print the answer.
printf("%lld\n", res);Algorithm implementation — string
Read the number as a string and the digits and as characters.
cin >> s >> a >> b;Replace all occurrences of character with character in the string .
replace(s.begin(), s.end(), a, b);Convert the string into a number of type long long.
res = stoll(s);Print the answer.
cout << res << endl;Java implementation
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
String s = con.next();
char a = con.next().charAt(0);
char b = con.next().charAt(0);
s = s.replace(a,b);
int flag = 0;
for(int i = 0; i < s.length(); i++)
if (s.charAt(i) != '0')
{
s = s.substring(i);
flag = 1;
break;
}
if (flag == 0) s = "0";
System.out.println(s);
con.close();
}
}Python implementation
Read the input data as strings.
s, a, b = input().split()Replace all occurrences of character with character in the string .
s = s.replace(a, b)Convert the string into an integer and print the result.
print(int(s))