1

I have had a look at a lot of previous questions put up, but still couldn't find anything that'll help me here. Here's a code I wrote to Reverse a sentence. I could have used split() function, but I tried to do without it anyways.

s='abcdef ghij klmn op qrst uv w xy z'
s=s[::-1]
print s
j=0
p=''
while(j<len(s)):
    a=''
    while(s[j]!=''):
        a=a+s[j]
        j+=1
    p.append(a[::-1])
    j+=1
print p

It gives me a string index out of range error in the while bracket. Why?

Thanks a lot for the help.

3
  • 1
    Your code isn't very readable. Consider using more descriptive names, and more whitespace around operators. Commented Oct 1, 2012 at 17:25
  • 2
    Will while(s[j] != '') ever evaluate to false while j less than len(S)? If not, when j gets to equal len(S) .. it'll crash!! Commented Oct 1, 2012 at 17:25
  • You create a lot of intermediate strings. You should work with lists. Commented Oct 1, 2012 at 17:32

4 Answers 4

2

Because in the second while loop you're incrementing j without checking if you're at the end yet.

Also, s[j]!='' will always be true for strings. If you can use the index operator on a string it means that there are characters. Otherwise there are none.

For example:

s = ''
s[0]  # IndexError, there are no characters so there can be no index

s = 'x'
s[0]  # Will be x and s[1] will give the same error as above

A little simpler version of your code (not really Pythonic, would be nicer to use lists and use ' '.join()):

s = 'abcdef ghij klmn op qrst uv w xy z'
print s

p = ''
i = 0
word = ''
while i < len(s):
    c = s[i]
    if c == ' ':
        if p:
            p = word + ' ' + p
        else:
            p = word

        word = ''
    else:
        word += c
    i += 1

print p

And the clean/simple Pythonic version with split:

s = 'abcdef ghij klmn op qrst uv w xy z'
print s
p = ' '.join(s.split()[::-1])
print p
Sign up to request clarification or add additional context in comments.

Comments

1

Your issue is with this inner loop:

while(s[j]!=''):
    a=a+s[j]
    j+=1

This loop allows j to exceed the length of s, you probably want to add an additional condition here to prevent this (I also removed the unnecessary parentheses):

while j < len(s) and s[j] != '':
    a=a+s[j]
    j+=1

1 Comment

Thank you. And I realised another quite stupid error. I was trying to append to a string. :)
1

I think you want to do this: -

s='abcdef ghij klmn op qrst uv w xy z'
s=s[::-1]
print s
j=0
p=[]
while(j<len(s)):
    a=''
    while(j<len(s) and s[j]!=' '):
        a=a+s[j]
        j+=1
    p.append(a[::-1])
    j+=1
print ' '.join(p)

1 Comment

Exactly. Only, I wasn't aware of the join function. Thanks.
0
while(s[j]!=''):
        a=a+s[j]
        j+=1

Here's is the problem.. Your last character is z.. When it reaches there, your while condition is true.. And it tries increment j to next index.. Which is out of bound..

You can probably move this condition also to your outer while loop.. Because you need to check both the conditions at the same time... Both the conditions must be true in your case..

Comments

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.