3

The function of this code is to capitalize letter that appears in the even indexed and lowercase the letter that appears in the odd indexed. Moreover, if whitespace appears between the words, the index has to be reset to 0. As you can see, the first and the second words are executed correctly. Whereas, the third word is incorrect. Instead of C and S being capitalized, 0 and 2 index respectively, A and E are capitalized.

string = 'Weird string case'
result = ''
i=0
for m in string:
    if(i%2==0):
       result = result+m.upper()
       i+=1
    elif(m==' '):
       result = result + m
       i=0
    else:
       result = result + m.lower()
       i+=1
    print(result)

Current Output

 WeIrD StRiNg cAsE

Expected Output

 WeIrD StRiNg CaSe

6 Answers 6

1

You should test if m is a space first; otherwise if the space occurs when i is even then i would not be reset:

string = 'Weird string case'
result = ''
i=0
for m in string:
    if(m==' '):
       result = result + m
       i=0
    elif(i%2==0):
       result = result+m.upper()
       i+=1
    else:
       result = result + m.lower()
       i+=1
    print(result)
Sign up to request clarification or add additional context in comments.

Comments

0

This seems to have fixed it.

string = 'Weird string case'
result = ''
i=0
for m in string:
    if(i%2==0):
       result = result+m.upper()
       i+=1
    elif(m==' '):
       result = result + m
       i=1
    else:
       result = result + m.lower()
       i+=1
    print(result)

Comments

0

I was not explicitly asked for, but we could tokenize the string first. This makes it case the individual words:

import re
string = 'Weird string case'
result = ''
for word in re.split("\\s+", string):
    for index, char in enumerate(word, 1):
        result = result+char.lower() if index%2==0 else result + char.upper()
    result += " "
print(result)

A regular string.split(" ") works as well. I've just used regex for demonstration purpose.

Comments

0

little swaps can solve the problem:

string = 'Weird string case'
result = ''
i=0
for m in string:
    if(i%2!=0):
       result = result+m.lower()
       i+=1
    elif(m==' '):
       result = result + m
       i=0
    else:
       result = result + m.upper()
       i+=1
    print(result)

Or have a two-liner, (could be one very long tho), a list comprehension and a map and two joins:

s=''.join([i.lower() if idx%2 else i.upper() for idx,i in enumerate(string)]).split()
print(' '.join(map(lambda x: x.swapcase() if x[0].islower() else x,s)))

Comments

0

If we enumerate string we can trim down our if, elif, else statement to just an if, else that would be not i % 2 for our upper.() and else for lower. We can append them to a list and then use ''.join(res) to print the final product. Also map is an option and list comprehension

map

s = 'Weird string case'
res = list(map(lambda x: x.upper() if not s.index(x) % 2 else x.lower(), s))
print(''.join(res))

list comprehension

res = [v.upper() if not i % 2 else v.lower() for i, v in enumerate(s)]
print(''.join(res))

Full loop:

res = []

for i, v in enumerate(s):
    if not i % 2:
        res.append(v.upper())
    else:
        res.append(v.lower())

print(''.join(res))
WeIrD StRiNg cAsE

Comments

0

I would use another list

    string = 'Weird string case'
    words = string.split(" ") #This creates a list of substrings from your original string
    result = ''
    for word in words:
        for m in string:
            if(i%2==0):
               result = result+m.upper()
               i+=1
            elif(m==' '):
               result = result + m
               i=0
            else:
               result = result + m.lower()
               i+=1
           `enter code here` print(result)

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.