Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn uppercase to lowercase using the charAt method?

Tags:

java

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);
}
like image 882
javip Avatar asked Oct 28 '12 21:10

javip


People also ask

How do I convert a char to lowercase?

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.

How do I convert a char to lowercase in Java?

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.

What does the charAt () method will do?

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.

How do I convert upper to lower in Java?

The toUpperCase() method converts a string to upper case letters. Note: The toLowerCase() method converts a string to lower case letters.


3 Answers

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));
}
like image 180
Rohit Jain Avatar answered Oct 26 '22 03:10

Rohit Jain


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;
     }
}
like image 45
dan Avatar answered Oct 26 '22 02:10

dan


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());
}
like image 1
case1352 Avatar answered Oct 26 '22 03:10

case1352