convert uppercase to lowercase in java without using string function

Let’s learn program to convert uppercase to lowercase in java without using string function.

convert uppercase to lowercase in java without using string function

To convert a string from uppercase to lowercase basically we use toLowerCase() method of String class.

But here in this post we are not using string function.

Also read – string concatenation in java

Instead we are iterating given string for lowercase and uppercase characters.

Now if we there is lowercase character then we know ASCII value of ‘b’ is 98 and that of uppercase character ‘B’ is 66.

Also read – string constructors in java

Here the difference between uppercase and lowercase character is 32. So we have to add 32 to uppercase character.

Now let’s see program to convert uppercase to lowercase without using string function.

import java.util.Scanner;
public class WithoutStringFunction
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter any string to convert to lowercase: ");
      String strInput = sc.nextLine();
      char[] ch = strInput.toCharArray();
      for(int a = 0; a < ch.length; a++)
      {
         if(ch[a] >= 'A' && ch[a] <= 'Z')
         {
            ch[a] = (char)((int)ch[a] + 32);
         }
      }
      System.out.println("string in lowercase is: ");
      for(int a = 0; a < ch.length; a++)
      {
         System.out.print(ch[a]);
      }
      sc.close();
   }
}


Output:

Please enter any string to convert to lowercase:
HELLO World Java
string in lowercase is:
hello world java

Please enter any string to convert to lowercase:
HELLO WORLD core JAVA
string in lowercase is:
hello world core java