Given a string and a character, write a Python program to replace multiple occurrences of the given character by a single character.
Here's a Python program that replaces multiple occurrences of a character with a single occurrence:
# function to remove multiple occurrences of a character def remove_duplicates(s): if len(s) < 2: return s result = s[0] for i in range(1, len(s)): if s[i] != s[i-1]: result += s[i] return result # test the function s = "abbcccddddeeeee" result = remove_duplicates(s) print(result)
This program defines a function called remove_duplicates
that takes a string s
as input and returns the string with multiple occurrences of a character replaced by a single occurrence. The function works by iterating over the characters of the string and appending each character to a result string if it is different from the previous character.
In the main part of the program, the function is called with a test string "abbcccddddeeeee" and the result is printed to the console. The output of the program should be the string "abcde".