Dado un String S , una array de caracteres ch[] , un número N y un carácter de reemplazo, la tarea es reemplazar cada enésima aparición de cada carácter de la array de caracteres ch[] en la string con el carácter de reemplazo dado.
Entrada: S = «GeeksforGeeks», ch[] = {‘G’, ‘e’, ’k’}, N = 2, replace_character = ‘#’ Salida: Ge#ksfor#ee#s Explicación:
en la
string
dada S, la segunda aparición de ‘G’, ‘e’, ’K’ se reemplaza con ‘#’
Entrada: S = abcdeahu, ch[] = {‘a’, ‘d’, ‘u’}, N = 1, replace_character = ‘#’ Salida: #bc#
eah #
Explicación:
En la string dada S, la primera la ocurrencia de ‘a’, ‘d’, ‘u’ se reemplaza con ‘#’
Método 1: Enfoque ingenuo
En este enfoque, la idea general es almacenar cada N-ésimo índice de ocurrencia de cada carácter en otra array y reemplazarlos con el otro carácter dado.
A continuación se muestra la implementación del enfoque anterior.
Python
# Python implementation to replace nth # occurrence of the every character # in a string # Function to replace the Nth occurrence # of the character of string def replacer(initial_string, ch, replacing_character, occurrence): # breaking a string into it's # every single character in list lst1 = list(initial_string) lst2 = list(ch) # List to store the indexes in which # it is replaced with the # replacing_character lst3 = [] # Loop to find the Nth occurrence of # given characters in the string for i in lst2: if(lst1.count(i)>= occurrence): count = 0 for j in range(0, len(initial_string)): if(i == lst1[j]): count+= 1 if(count == occurrence): lst3.append(j) for i in lst3: # Replacing that particular index # with the requested character lst1[i] = replacing_character print(''.join(lst1)) # Driver Code: if __name__ == '__main__': initial_string = 'GeeksforGeeks' ch = ['G', 'e', 'k'] occurrence = 2 replacing_character = '#' replacer(initial_string, ch, replacing_character, occurrence)
Ge#ksfor#ee#s
Método 2: usar el método find()
En este enfoque, la idea es usar la función find() para encontrar la enésima ocurrencia del carácter en la string S dada y reemplazarla con otro carácter dador.
A continuación se muestra la implementación del enfoque anterior.
Python
# Python implementation to replace nth # occurrence of the every character # in a string using find() function # Function to replace the Nth occurrence # of the character of string def replacer(initial_string, ch, replacing_character, occurrence): # breaking a string into # it's every single character lst1 = list(initial_string) lst2 = list(ch) # Loop to find the occurrence # of the character in the string # and replace it with the given # replacing_character for i in lst2: sub_string = i val = -1 for i in range(0, occurrence): val = initial_string.find(sub_string, val + 1) lst1[val] = replacing_character print(''.join(lst1)) # Driver Code: if __name__ == '__main__': initial_string = 'GeeksforGeeks' ch = ['G', 'e', 'k'] occurrence = 2 replacing_character = '#' replacer(initial_string, ch, replacing_character, occurrence)
Ge#ksfor#ee#s
Método 3: Usando el método beginwith()
En este enfoque, la idea es usar la función beginwith() de python para encontrar el índice del carácter donde la ocurrencia del carácter es igual a la enésima ocurrencia dada y luego reemplazarla con la dado el carácter de reemplazo.
A continuación se muestra la implementación del enfoque anterior.
Python
# Python implementation to replace nth # occurrence of the every character # in a string # Function to replace the Nth occurrence # of the character of string def replacer(initial_string, ch, replacing_character, occurrence): # breaking a string into # it's every single character lst1 = list(initial_string) lst2 = list(ch) # Loop to find the occurrence # of the character in the string # and replace it with the given # replacing_character for j in lst2: sub_string = j checklist = [i for i in range(0, len(initial_string)) if initial_string[i:].startswith(sub_string)] if len(checklist)>= occurrence: lst1[checklist[occurrence-1]] = replacing_character print(''.join(lst1)) # Driver Code: if __name__ == '__main__': initial_string = 'GeeksforGeeks' ch = ['G', 'e', 'k'] occurrence = 2 replacing_character = '#' replacer(initial_string, ch, replacing_character, occurrence)
Ge#ksfor#ee#s
Publicación traducida automáticamente
Artículo escrito por RISHU_MISHRA y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA