- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to replace all digits with characters using Python
Suppose we have an alphanumeric string s that contains lowercase English letters in its even positions and digits in its odd positions. Consider an operation shift(c, x), where c is any character and x is a number (digit), this will find the xth character after c. So, for example, shift('p', 5) = 'u' and shift('a', 0) = 'a'. Now for every odd index i, we want to replace the digit s[i] with shift(s[i-1], s[i]). We have to find s after replacing all digits.
So, if the input is like s = "a2b1d4f3h2", then the output will be "acbcdhfihj" because
shift('a', 2) = 'c'
shift('b', 1) = 'c'
shift('d', 4) = 'h'
shift('f', 3) = 'i'
shift('h', 2) = 'j'
To solve this, we will follow these steps −
res:= blank string
for i in range 0 to size of s, do
if s[i] is a digit, then
res := res concatenate character from (ASCII s[i] + ASCII of s[i-1])
res := res concatenate s[i]
Let us see the following implementation to get better understanding −
def solve(s): res="" for i in range(len(s)): if s[i].isdigit(): res+= chr(int(s[i])+ord(s[i-1])) else: res+=s[i] return res s = "a2b1d4f3h2" print(solve(s))
- Program to replace all question symbols to avoid consecutive repeating characters in Python
- Python program to replace all Characters of a List except the given character
- Python Program – Strings with all given List characters
- How to replace Digits into String using Java?
- Find all strings formed from characters mapped to digits of a number in Python
- Python Program to Accept Three Digits and Print all Possible Combinations from the Digits
- Program to count submatrices with all ones using Python
- Python Program to Replace all Occurrences of ‘a’ with $ in a String
- Program to replace all the characters in of a file with '#' except a particular word in Java
- Python – Strings with all given List characters
- C program to write all digits into words using for loop
- Python program to find all duplicate characters in a string
- Java Program to replace the first 10 characters in JTextArea
- Java Program to replace all occurrences of given String with new one