Skip to content

Commit af1e20e

Browse files
Program 10
1 parent bf635f3 commit af1e20e

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

Lab Cycle 2/program10.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Program to Get a string from an input string where all occurrences of the first
2+
# character are replaced with ‘$’, except the first character. [eg: onion -> oni$n]
3+
4+
# Function to replace the occurence of a character with $
5+
def replace_char(str):
6+
unique_char = set() # To store unique characters
7+
for i in range(0,len(str)): # loop to get the unique character
8+
if str[i] in unique_char:
9+
str=str[:i]+"$"+str[i+1:] # replace the character with $
10+
else:
11+
unique_char.add(str[i]) # add the unique character to the set
12+
return str
13+
14+
str = input("Enter a string: ")
15+
print(replace_char(str)) # Call the function
16+

0 commit comments

Comments
 (0)