0

I'm used to code in C, and I'm trying to pass the functions I implemented to Python, and I'm new to the language. This function here works perfecly in C, but it's giving me overflow when I enter the following input:

"Rafael af rosa"

What can be wrong?

def substr(str1,str2,str3):
    """The function analyzes a string and replaces a part with another."""
    #str1 is the function which the part will be replaced.
    #str2 is the part to be replaced.
    #str3 is the string that will replace str2 in str1.
    n=0
    while n<len(str1):
        if str1[n]==str2[0]:
            k=0
            l=n
            d=1
            while k<len(str2):
                if str1[l]!=str2[k]:
                    d=0
                    break
                k+=1
                l+=1
            if d==1:
                if len(str2)>len(str3):
                    l=0
                    while l<(len(str2)-len(str3)):
                        k=n
                        while str1[k]!=0:
                            str1[k]=str1[k+1]
                            k+=1
                        l+=1
                if len(str3)>len(str2):
                    l=0
                    while l<(len(str3)-len(str2)):
                        k=len(str1)
                        while k>=n:
                            str1[k+1]=str1[k]
                            k-=1
                        l+=1
                k=n
                l=0
                while l<len(str3):
                    str1[k]=str3[l]
                    k+=1
                    l+=1
                n+=len(str3)-1
        n+=1
    return str1

str1=input()
str2=input()
str3=input()
strf=substr(str1,str2,str3)
print(strf)

2 Answers 2

3

Or you can simply do:

'Rafael'.replace('af', 'rosa')
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, I feel like an idiot. I knew I was overlooking some easy way of doing it in my answer--I must be tired. Anyway, +1 for you.
Here is the python source code where replace() lives. It calls a few different versions depending on the case. svn.python.org/projects/python/trunk/Objects/stringobject.c
0

Welcome to Python programming! As someone who moved from C to Delphi to Python, I can say that your new language is both very useful and fun.

However, Python is very different from C and has different ways of doing things. Rather than translating your C code to Python, you would be better off learning Python and redoing your code from scratch.

One difference is that Python's strings are immutable. That means you cannot change part of the string, such as a character. You must replace the entire string with a new string. Thus your statement str1[k]=str1[k+1] will not work at all.

Python has very strong built-in string-handling capabilities. You could use regular expressions, for example, to do your replacement easily. If you want to stick to Python's built-in capabilities, you could do

place = str1.find(str2)
strf = str1[:place] + str3 + str1[place+len(str2):]
print(strf)

and that would print

Rrosaael

which I think is what you wanted.

(Wow, I feel like an idiot.) Even better, do what @BertKellerman suggests:

'Rafael'.replace('af', 'rosa')

1 Comment

Thank you a lot, it seems adapting the code won't be the solution then haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.