开发者

Write a function which finds a substring in a string and replaces all such occurrences with another string? [closed]

开发者 https://www.devze.com 2023-01-28 18:17 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

Write a function which finds a substring in a string and replaces all such occurrences with another string. Prototype of the function :

      char* FindReplace(char* s开发者_C百科rc, char* find, char* replace);

Test case 1:

INPUT:

SRC[] = "SIVASAI".

find[] = "AS"

Replace[] = "ASAS"

OUTPUT:

SRC[] = "SIVASASAI"

Test case 2:

SRC[] = "SIVASAI".

find[] = "VASA"

Replace[] = "A"

OUTPUT:

SRC[] = "SIAI"

Solution should be Time Efficient .


You can use:
1) Knuth–Morris–Pratt algorithm: http://en.wikipedia.org/wiki/Knuth-Morris-Pratt_algorithm
2) Boyer-Moore algorithm http://en.wikipedia.org/wiki/Boyer-Moore_string_search_algorithm
3) Rabin-Karp algorithm http://en.wikipedia.org/wiki/Rabin-Karp_string_search_algorithm

0

精彩评论

暂无评论...
验证码 换一张
取 消