Home > Net >  python program that takes input and print data as sequence of upper and lowercase
python program that takes input and print data as sequence of upper and lowercase

Time:07-17

Write a python program that takes a string as an input from the user and then modifies the string in such a way that the string always starts with an uppercase letter and the case of each subsequent letter is the opposite of the previous letter (uppercase character followed by a lowercase character followed by an uppercase character and so on). Finally, the modified string is printed to show the user.

I'm not allowed to use uppercase and lowercase function

What I tried so far :

user=input()
s=""
upper=True
if user[0]>='a' and user[0]<='z':
  s =chr(ord(user[0])-32)
else:
  s =user[0]
  # s =chr(ord(user[0]) 32)
for i in range(1,len(user)):
  if upper==True:
    if user[i]>='A' and user[i]<='Z':
      upper=False
      s =chr(ord(user[i]) 32)
    elif user[i]>='a'and user[i]<='z':
      upper=False
      s =user[i]
      # s =chr(ord(user[i])-32)
    else:
      s =chr(ord(user[i])-32)
      # s =user[i]
  else:
    if user[i]>='A' and user[i]<='Z':
      upper=True
      s =user[i]
      # s =chr(ord(user[i]) 32)
    elif user[i]>='a' and user[i]<='z':
      upper=True
      # s =user[i]
      s =chr(ord(user[i])-32)
    else:
      # s =chr(ord(user[i])-32)
      s =user[i]
print(s)

after running its printing like

PyThOn PrOgRaMmInG�iS�vErY�eAsY

now don't know why there is question mark. It suppose to take space

CodePudding user response:

you can try to define "lower" and "upper" functions yourself

def isup(s):
    # return not islow(s) # uncomment if specials chars are considered as upper
    return ord(s) in range(ord('A'),ord('Z'))

def islow(s):
    # return ord(s) in range(ord('a'),ord('z')) # uncomment too
    return not isup(s)

def lower(s):
    if isup(s):
        return chr(ord(s)-32)
    else: return s
def upper(s):
    if islow(s):
        return chr(ord(s)-32)
    else: return s

then the code would be:

inp = "hello world"
out = inp[0]
for i in range(1,len(inp)):
    curent = inp[i]
    prev = out[-1]
    if isup(prev):out =lower(curent)
    else:out =upper(curent)

print(out)

CodePudding user response:

You can XOR a letter with the space character (which has an ASCII value of 32) to invert its case:

def alternate_case(s: str) -> str:
    result = []
    i = 0
    for ch in s:
        is_lower_case = ord('a') <= ord(ch) <= ord('z')
        is_upper_case = ord('A') <= ord(ch) <= ord('Z')
        if not is_lower_case and not is_upper_case:
            result.append(ch)
            continue
        if i % 2 == 0 and is_lower_case or is_upper_case:
            result.append(chr(ord(ch) ^ ord(' ')))  # Invert case.
        else:
            result.append(ch)
        i  = 1
    return ''.join(result)


def main() -> None:
    print(alternate_case('hello'))
    print(alternate_case('python programming is very easy'))


if __name__ == '__main__':
    main()

Output:

HeLlO
PyThOn PrOgRaMmInG iS vErY eAsY

CodePudding user response:

I suggest using Sash Sinha answer (using XOR to flip the ASCII 6th bit value) with a generator expression that iterate the string while handling next letter case type according to the last alphabetic character. Then, call it with a join of list comprehension line that return back the transformed string.

data_str : str = "! python prograMMING ==> is very easy !!"

def flip_case(ch: chr) -> chr:
    if ch.isalpha():
        return chr(ord(ch) ^ ord(' '))
    return ch

def case_flipping_gen(input_str : str) -> chr:
    case_up_flag = False
    for ch in input_str:
        is_lower_case = ord('a') <= ord(ch) <= ord('z')
        if (ch.isalpha()): case_up_flag = not(case_up_flag)
        yield flip_case(ch) if (case_up_flag and is_lower_case) or (not case_up_flag and not is_lower_case) else ch

print (''.join([x for x in case_flipping_gen(data_str)]))

Output:

! PyThOn PrOgRaMmInG ==> iS vErY eAsY !!

BTW, I used isalpha(). If that also not allowed, use char ord() conditions, like 'is_lower_case' and 'is_upper_case' in Sash Sinha answer...

  • Related