Conversion of string to upper case without inbuilt methods

前端 未结 7 1683
轻奢々
轻奢々 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:38

    The below-simplified code help to convert Lower-case alphabets to Upper-case alphabets using a simple calculation

    code :

    def toUppercase(string):
        convertedCharacter = ''
        for i in string: 
             convertCharacter += chr( ( (ord(i)) -32) ) 
        return convertCharacter
    
    0 讨论(0)
  • 2021-01-29 08:39
    char=input("Enter lowercase word :")
    for letter in char:
        s=ord(letter)
        if s>=97 and s<=122:
            print(chr(s-32),end=" ")
    
    0 讨论(0)
  • 2021-01-29 08:41

    The best way, in my opinion is using a helper string, representing the alphabet, if you do not want to use chr() and ord():

    def toUppercase(s):
        alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
        result = ''
        for x in s:
            if x not in alphabet or alphabet.index(x)>=26:
                result += x
            else:
                result += alphabet[alphabet.index(x)+26]
        return result
    

    This also handles punctuation such as ; or ..


    Update:

    As per the OP's request, this is a version without index():

    def toUppercase(s):
        alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
        result = ''
        for x in s:
            for pos in range(52):
                if alphabet[pos] == x:
                    i = pos
            if x not in alphabet or i>=26:
                result += x
            else:
                result += alphabet[i+26]
        return result
    
    print(toUppercase('abcdj;shjgh'))
    
    0 讨论(0)
  • 2021-01-29 08:46

    ord()- Return the Unicode code point for a one-character string.

    You have to send a one character string as an argument. Here, you are sending the string 'abcd' which has 4 characters which is causing the issue. Send each character separately to the function and thus do 4 calls to the function to get the result.

    0 讨论(0)
  • 2021-01-29 08:49

    You need to execute ord() for each character of your input string. instead of the input string:

    def uppercase(str_data):
        return ''.join([chr(ord(char) - 32) for char in str_data if ord(char) >= 65])
    
    print(uppercase('abcdé--#'))
    >>> ABCDÉ
    

    Without join:

    def uppercase(str_data):
        result = ''
        for char in str_data:
            if ord(char) >= 65:
                result += chr(ord(char) - 32)
        return result
    print(uppercase('abcdé--#λ'))
    >>> ABCDÉΛ
    
    0 讨论(0)
  • 2021-01-29 08:55

    Here is a program to convert the string to uppercase without using inbuilt functions:

    Str1=input("Enter the string to be converted uppercase: ")
    
    for i in range (0,len(Str1)):
    
       x=ord(Str1[i])
       if x>=97 and x<=122:
           x=x-32
       y=chr(x)
       print(y,end="")
    
    0 讨论(0)
提交回复
热议问题