字母小写转大写的函数
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]
字母小写转大写的函数