字母小写转大写的函数_在Java中不使用任何库函数的情况下将小写字母转换为大写字母...

字母小写转大写的函数

Given a string and we have to convert it from lowercase to uppercase.

给定一个字符串,我们必须将其从小写转换为大写。

Examples:

例子:

    Input:
    IncludeHelp.com
    Output:
    INCLUDEHELP.COM

    Input:
    [email protected]
    Output:
    [email protected]

小写到大写转换 (Lowercase to uppercase conversion)

To convert a lowercase alphabet to uppercase alphabet – we can subtract 32 from lowercase alphabet's ASCII code to make it an uppercase alphabet (because the difference between a lowercase alphabet ASCII and an uppercase alphabet ASCII is 32).

要将小写字母转换为大写字母 –我们可以从小写字母的ASCII码中减去32 ,以使其成为大写字母(因为小写字母ASCII与大写字母ASCII之间的差是32 )。

In the below code, we created a user-defined function LowerToUpper() that will accept a string and returns string having uppercase characters. To convert lowercase alphabets of the string to uppercase alphabets, we are extracting characters one by one from the string using String.charAt() function and checking whether the character is a lowercase alphabet, if it is a lowercase alphabet, we are subtracting 32 to make it uppercase, else no change. Thus, only lowercase alphabets will be converted to uppercase alphabets, the rest of the characters like uppercase alphabets, digits and special characters will remain the same.

在下面的代码中,我们创建了一个用户定义的函数LowerToUpper() ,该函数将接受一个字符串并返回包含大写字符的字符串。 要将字符串的小写字母转换为大写字母 ,我们使用String.charAt()函数从字符串中逐个提取字符,并检查该字符是否为小写字母,如果是小写字母,我们将32减去将其设为大写,否则保持不变。 因此,只有小写字母会转换为大写字母,其余字符(如大写字母,数字和特殊字符)将保持不变。

Java代码,用于小写到大写的转换 (Java code for lowercase to uppercase conversion )

// Lowercase to uppercase conversion without using 
// any library function in Java

public class Main {
    static String LowerToUpper(String s) {
        String result = "";
        char ch = ' ';
        for (int i = 0; i < s.length(); i++) {
            
            //check valid alphabet and it is in lowercase
            if (s.charAt(i) >= 'a' && s.charAt(i) <= 'z') {
                ch = (char)(s.charAt(i) - 32);
            }
            //else keep the same alphabet or any character
            else {
                ch = (char)(s.charAt(i));
            }
            
            result += ch; // concatenation, append c to result
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(LowerToUpper("IncludeHelp.com"));
        System.out.println(LowerToUpper("www.example.com"));
        System.out.println(LowerToUpper("[email protected]"));
        System.out.println(LowerToUpper("[email protected]"));       
    }
}

Output

输出量

INCLUDEHELP.COM
WWW.EXAMPLE.COM
[email protected]
[email protected]


翻译自: https://www.includehelp.com/java-programs/lowercase-to-uppercase-conversion-without-using-any-library-function-in-java.aspx

字母小写转大写的函数

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值