My issue
compiler having issue
My code
def find_kth_occurrence(s1, c1, k):
count = 0 # Initialize occurrence counter
for index, char in enumerate(s1): # Loop through the string with index
if char == c1: # Check if current character matches c1
count += 1 # Increment count for each occurrence
if count == k: # Check if we have found the k-th occurrence
return index # Return the index of the k-th occurrence
return -1 # Return -1 if k-th occurrence does not exist
# Taking input from the user
input_string = input("Enter the string (s1): ")
character = input("Enter the character (c1): ")
k = int(input("Enter the integer (k): "))
# Finding the position of the k-th occurrence
position = find_kth_occurrence(input_string, character, k)
# Printing the result
print(position)
Learning course: Data structures & Algorithms lab
Problem Link: Find Kth Character Position in Data structures & Algorithms lab