I need to create a function that will make all uppercase characters lowercase using the charAt
method. I've tried to use typecasting to change the int
values to char
but got lost after that.
/********************************************************************************
This function will calculate the integer average of characters in the array
********************************************************************************/
public static void lowerCase(char[] letter) {
char mean;
mean = (char) ((int) ch + 32);
}
toLowerCase(char ch) converts the character argument to lowercase using case mapping information from the UnicodeData file. Note that Character. isLowerCase(Character. toLowerCase(ch)) does not always return true for some ranges of characters, particularly those that are symbols or ideographs.
The toLowerCase() method is a static method in the Character class in Java, which is used to convert a character to lowercase. The input to this function is a character. If you need to convert a string to lowercase, refer to the String. toLowerCase method.
The charAt() method returns the character at the specified index in a string. The index of the first character is 0, the second character is 1, and so on.
The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.
Oh, actually you don't need to check it using charAt. Just convert everything to lowercase. That will not affect the character that are already in lowercase, and convert the uppercase characters to lowercase. That's what you need.
You don't need to convert your character array
to string object
and then use String.toLowerCase
method, because it internally uses Character.toLowerCase
method only.
public static void average( char [] letter ) {
for (int i = 0; i < letter.length; i++) {
letter[i] = Character.toLowerCase(letter);
}
System.out.println(Arrays.toString(letter));
}
If you like to use only charAt, you can try:
String test = "fasdWADFASD242134";
StringBuilder result = new StringBuilder(test);
for (int i = 0; i < test.length(); i++) {
char ch = test.charAt(i);
result.setCharAt(i, ch >= 'A' && ch <= 'Z' ? (char) (ch + 'a' - 'A') : ch);
}
System.out.println("result = " + result);
If you have an char array, you can use:
public static void toLower(char[] letter){
for (int i = 0; i < letter.length; i++) {
char ch= letter[i];
letter[i]= ch >= 'A' && ch <= 'Z' ? (char) (ch + 'a' - 'A') : ch;
}
}
If you dont have to use charAt()...
public static void average( char [] letter )
{
String str = new String(letter);
System.out.println("The value is "+str.toUpperCase());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With