Conversion of string to upper case without inbuilt methods

前端 未结 7 1684
轻奢々
轻奢々 2021-01-29 08:28

I am trying to perform conversion from a lowercase to uppercase on a string without using any inbuilt functions (other than ord() and char()). Following the logic presented on a

相关标签:
7条回答
  • 2021-01-29 08:55
    def uppercase(str_data):
       ord('str_data')
       str_data = str_data -32
       chr('str_data')
       return str_data
    print(uppercase('abcd'))
    

    in this code ord takes single character as an argument but you have given more than one that's why it's showing an error. Take single character at a time convert it to upper case and make a single string like below.

    def convert_to_lower(string):
        new="" 
        for i in string:
           j=ord(i)-32  #we are taking the ascii value because the length of lower 
                 #case to uppercase is 32 so we are subtracting 32
    
           if 97<=ord(i)<=122 :  #here we are checking whether the charecter is lower
                          # case or not if lowercase then only we are converting into
                          #uppercase
               new=new+chr(j)
           else:  #if the character is not the lowercase alplhaber we are taking as it is
               new=new+i
        print(new)
    
    convert_to_lower("hello world")
    
    0 讨论(0)
提交回复
热议问题