Replace the second occurrence of a character

In this program, we will take a string as input and replace the second occurrence of any char with the special character $.

Steps to solve the program
  1. Take a string as input and create an empty string.
  2. Using For loop add each character of the given string to the empty string.
  3. If a character repeats then do not add that character add $ instead.
  4. Print the output.
				
					#Input string
str1 = "Programming"
result = ''

#checking for repeated char
for char in str1:
    if char in result:
        result = result + "$"
    else:
        result = result + char

#Printing output
print("Result :", result)
				
			

Output :

				
					Result : Prog$am$in$
				
			

calculate the length of a string with loop logic.

swap the last character of a given string.

Leave a Comment