0

I am having difficulty converting a camel case string into separate words and appending these into a list. It almost completes the code but it gives an IndexError: string index out of range. Please could anyone help with this?? The output when run is:

['This']
['This', 'Is']
['This', 'Is', 'A']
['This', 'Is', 'A', 'Camel']
['This', 'Is', 'A', 'Camel', 'Case']
Traceback (most recent call last):
for i in string[char]:
IndexError: string index out of range

Picture of code

List = []
string = "ThisIsACamelCaseString"
newstring = ""
count = 0
char = 0
null = 0

for i in string[char:]:
    if i == i.upper():
        newstring = newstring + i
        count += 1
        char += 1
    for i in string[char]:      **< error here**
        if i == i.upper() and char == 1:
            null += 1
        elif i == i.lower():
            newstring = newstring + i
            char += 1
            count += 1      
        elif i == i.upper() and count > 0:
            List.append(newstring)
            print(List)
            newstring = ""
            break
Amash Mir
  • 9
  • 2
  • it prints the entire string but not the last word "String" from the actual string – Amash Mir Feb 25 '17 at 19:00
  • This is like a doctor trying to diagnose an illness by looking through a hole in a sheet. – Peter Wood Feb 25 '17 at 19:00
  • I would recommend adding edits to your post, and updating with the code opposed to a picture. – GedAWizardofEarthSea Feb 25 '17 at 19:01
  • How many times is it possible to increase `char`? – Peter Wood Feb 25 '17 at 19:02
  • Char is increased 16 times to give a value of 16. This value is then set in the range for the for loop. The loop iterates through the string starting at position 16 but it fails. This should not cause an error. It literally prints all the words from the string yet crashes with an indexerror at position 16 of the string. – Amash Mir Feb 25 '17 at 19:20

1 Answers1

0

This is my way of accomplishing this task. I have a counter called lastword, which keeps track of where the previous 'word' ended. Then I loop through the entire string letter by letter (like you do) and if I get to an uppercase letter, then you reset the lastword variable to the current position.

x = "ThisIsACamelCaseString"

#list to store results
sentence = []            
lastword = 0
#count keeps track of what position we are at
for count, letter in enumerate(x):
    #I use the isupper() methd to see if letter is uppercase
    if letter.isupper():
        word = x[lastword:count]
        lastword = count
        #if word is needed because otherwise the first 'word' will be literally nothing
        if word:
            sentence.append(word)
print sentence
Abid Hasan
  • 648
  • 4
  • 10
  • Thank you Abid. appreciate the help. Is there any chance you could help me with the problem with my code though as I need to use that particular version in this instance. – Amash Mir Feb 25 '17 at 19:21
  • I tried your version of the code and it also did not print the last word which was String. – Amash Mir Feb 26 '17 at 03:45
  • You can add this line after the for loop: `sentence.append(x[lastword:]) ` – Abid Hasan Feb 26 '17 at 20:25