Convert a String from LowerTo Upper (vice-versa) without using type casting or built-in functions in Java...












-5















How can we convert a String from lower to upper (Vice-versa ) in Java
without using any built-in function or using any type casting ?










share|improve this question













closed as too broad by achAmháin, kryger, GriffeyDog, oleg.cherednik, gnat Nov 15 '18 at 5:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    A rather large loop through your String...if(str.charAt(i) == 'a'...) , or perhaps view the source code of the String class and find the method that does this already...or does this count as using the inbuilt charAt method? Or have a map of all characters lower case and upper case and have the values as the opposite.

    – achAmháin
    Nov 14 '18 at 15:25


















-5















How can we convert a String from lower to upper (Vice-versa ) in Java
without using any built-in function or using any type casting ?










share|improve this question













closed as too broad by achAmháin, kryger, GriffeyDog, oleg.cherednik, gnat Nov 15 '18 at 5:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 1





    A rather large loop through your String...if(str.charAt(i) == 'a'...) , or perhaps view the source code of the String class and find the method that does this already...or does this count as using the inbuilt charAt method? Or have a map of all characters lower case and upper case and have the values as the opposite.

    – achAmháin
    Nov 14 '18 at 15:25
















-5












-5








-5








How can we convert a String from lower to upper (Vice-versa ) in Java
without using any built-in function or using any type casting ?










share|improve this question














How can we convert a String from lower to upper (Vice-versa ) in Java
without using any built-in function or using any type casting ?







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 15:23









JayJay

64




64




closed as too broad by achAmháin, kryger, GriffeyDog, oleg.cherednik, gnat Nov 15 '18 at 5:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by achAmháin, kryger, GriffeyDog, oleg.cherednik, gnat Nov 15 '18 at 5:56


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 1





    A rather large loop through your String...if(str.charAt(i) == 'a'...) , or perhaps view the source code of the String class and find the method that does this already...or does this count as using the inbuilt charAt method? Or have a map of all characters lower case and upper case and have the values as the opposite.

    – achAmháin
    Nov 14 '18 at 15:25
















  • 1





    A rather large loop through your String...if(str.charAt(i) == 'a'...) , or perhaps view the source code of the String class and find the method that does this already...or does this count as using the inbuilt charAt method? Or have a map of all characters lower case and upper case and have the values as the opposite.

    – achAmháin
    Nov 14 '18 at 15:25










1




1





A rather large loop through your String...if(str.charAt(i) == 'a'...) , or perhaps view the source code of the String class and find the method that does this already...or does this count as using the inbuilt charAt method? Or have a map of all characters lower case and upper case and have the values as the opposite.

– achAmháin
Nov 14 '18 at 15:25







A rather large loop through your String...if(str.charAt(i) == 'a'...) , or perhaps view the source code of the String class and find the method that does this already...or does this count as using the inbuilt charAt method? Or have a map of all characters lower case and upper case and have the values as the opposite.

– achAmháin
Nov 14 '18 at 15:25














2 Answers
2






active

oldest

votes


















1














public static String toUpperCase(String str) {
char arr = str.toCharArray();

for (int i = 0; i < arr.length; i++)
if (arr[i] >= 'a' && arr[i] <= 'z')
arr[i] -= 'a' - 'A';

return new String(arr);
}

public static String toLowerCase(String str) {
char arr = str.toCharArray();

for (int i = 0; i < arr.length; i++)
if (arr[i] >= 'A' && arr[i] <= 'Z')
arr[i] += 'a' - 'A';

return new String(arr);
}





share|improve this answer

































    1














    You can use ASCII values to change cases. in your case converting lower case alphabets to upper case loweer case a has ASCII value 97 and z has 122 and if these number is exactly subtracted by 32 then it will be equal to ASCII value of its uppercase letter like 97-32=65 is ASCII value of A. Hope this helps.



    public class toUpperCase{
    public static void main(String args){

    toUpperCase(args[0]);
    }

    //for lower to upper case
    public static void toUpperCase(String a){

    for (int i = 0; i< a.length(); i++){

    char aChar = a.charAt(i);
    if (97 <= aChar && aChar<=122){
    aChar = (char)( (aChar - 32) );
    }

    System.out.print(aChar);
    }
    }
    //for upper to lower case
    public static void toUpperCase(String a){

    for (int i = 0; i< a.length(); i++){

    char aChar = a.charAt(i);
    if (65 <= aChar && aChar<= 97){
    aChar = (char)( (aChar + 32) );
    }

    System.out.print(aChar);
    }
    }
    }


    But, again it needs typecasting.
    for ASCII values check ASCII Table






    share|improve this answer


























    • Check your code, it does not works! aChar goes nowhere

      – oleg.cherednik
      Nov 14 '18 at 15:57











    • Thanks for letting me know we should print aChar

      – Bhandari
      Nov 14 '18 at 16:01


















    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    public static String toUpperCase(String str) {
    char arr = str.toCharArray();

    for (int i = 0; i < arr.length; i++)
    if (arr[i] >= 'a' && arr[i] <= 'z')
    arr[i] -= 'a' - 'A';

    return new String(arr);
    }

    public static String toLowerCase(String str) {
    char arr = str.toCharArray();

    for (int i = 0; i < arr.length; i++)
    if (arr[i] >= 'A' && arr[i] <= 'Z')
    arr[i] += 'a' - 'A';

    return new String(arr);
    }





    share|improve this answer






























      1














      public static String toUpperCase(String str) {
      char arr = str.toCharArray();

      for (int i = 0; i < arr.length; i++)
      if (arr[i] >= 'a' && arr[i] <= 'z')
      arr[i] -= 'a' - 'A';

      return new String(arr);
      }

      public static String toLowerCase(String str) {
      char arr = str.toCharArray();

      for (int i = 0; i < arr.length; i++)
      if (arr[i] >= 'A' && arr[i] <= 'Z')
      arr[i] += 'a' - 'A';

      return new String(arr);
      }





      share|improve this answer




























        1












        1








        1







        public static String toUpperCase(String str) {
        char arr = str.toCharArray();

        for (int i = 0; i < arr.length; i++)
        if (arr[i] >= 'a' && arr[i] <= 'z')
        arr[i] -= 'a' - 'A';

        return new String(arr);
        }

        public static String toLowerCase(String str) {
        char arr = str.toCharArray();

        for (int i = 0; i < arr.length; i++)
        if (arr[i] >= 'A' && arr[i] <= 'Z')
        arr[i] += 'a' - 'A';

        return new String(arr);
        }





        share|improve this answer















        public static String toUpperCase(String str) {
        char arr = str.toCharArray();

        for (int i = 0; i < arr.length; i++)
        if (arr[i] >= 'a' && arr[i] <= 'z')
        arr[i] -= 'a' - 'A';

        return new String(arr);
        }

        public static String toLowerCase(String str) {
        char arr = str.toCharArray();

        for (int i = 0; i < arr.length; i++)
        if (arr[i] >= 'A' && arr[i] <= 'Z')
        arr[i] += 'a' - 'A';

        return new String(arr);
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 16:21

























        answered Nov 14 '18 at 15:54









        oleg.cherednikoleg.cherednik

        6,92921118




        6,92921118

























            1














            You can use ASCII values to change cases. in your case converting lower case alphabets to upper case loweer case a has ASCII value 97 and z has 122 and if these number is exactly subtracted by 32 then it will be equal to ASCII value of its uppercase letter like 97-32=65 is ASCII value of A. Hope this helps.



            public class toUpperCase{
            public static void main(String args){

            toUpperCase(args[0]);
            }

            //for lower to upper case
            public static void toUpperCase(String a){

            for (int i = 0; i< a.length(); i++){

            char aChar = a.charAt(i);
            if (97 <= aChar && aChar<=122){
            aChar = (char)( (aChar - 32) );
            }

            System.out.print(aChar);
            }
            }
            //for upper to lower case
            public static void toUpperCase(String a){

            for (int i = 0; i< a.length(); i++){

            char aChar = a.charAt(i);
            if (65 <= aChar && aChar<= 97){
            aChar = (char)( (aChar + 32) );
            }

            System.out.print(aChar);
            }
            }
            }


            But, again it needs typecasting.
            for ASCII values check ASCII Table






            share|improve this answer


























            • Check your code, it does not works! aChar goes nowhere

              – oleg.cherednik
              Nov 14 '18 at 15:57











            • Thanks for letting me know we should print aChar

              – Bhandari
              Nov 14 '18 at 16:01
















            1














            You can use ASCII values to change cases. in your case converting lower case alphabets to upper case loweer case a has ASCII value 97 and z has 122 and if these number is exactly subtracted by 32 then it will be equal to ASCII value of its uppercase letter like 97-32=65 is ASCII value of A. Hope this helps.



            public class toUpperCase{
            public static void main(String args){

            toUpperCase(args[0]);
            }

            //for lower to upper case
            public static void toUpperCase(String a){

            for (int i = 0; i< a.length(); i++){

            char aChar = a.charAt(i);
            if (97 <= aChar && aChar<=122){
            aChar = (char)( (aChar - 32) );
            }

            System.out.print(aChar);
            }
            }
            //for upper to lower case
            public static void toUpperCase(String a){

            for (int i = 0; i< a.length(); i++){

            char aChar = a.charAt(i);
            if (65 <= aChar && aChar<= 97){
            aChar = (char)( (aChar + 32) );
            }

            System.out.print(aChar);
            }
            }
            }


            But, again it needs typecasting.
            for ASCII values check ASCII Table






            share|improve this answer


























            • Check your code, it does not works! aChar goes nowhere

              – oleg.cherednik
              Nov 14 '18 at 15:57











            • Thanks for letting me know we should print aChar

              – Bhandari
              Nov 14 '18 at 16:01














            1












            1








            1







            You can use ASCII values to change cases. in your case converting lower case alphabets to upper case loweer case a has ASCII value 97 and z has 122 and if these number is exactly subtracted by 32 then it will be equal to ASCII value of its uppercase letter like 97-32=65 is ASCII value of A. Hope this helps.



            public class toUpperCase{
            public static void main(String args){

            toUpperCase(args[0]);
            }

            //for lower to upper case
            public static void toUpperCase(String a){

            for (int i = 0; i< a.length(); i++){

            char aChar = a.charAt(i);
            if (97 <= aChar && aChar<=122){
            aChar = (char)( (aChar - 32) );
            }

            System.out.print(aChar);
            }
            }
            //for upper to lower case
            public static void toUpperCase(String a){

            for (int i = 0; i< a.length(); i++){

            char aChar = a.charAt(i);
            if (65 <= aChar && aChar<= 97){
            aChar = (char)( (aChar + 32) );
            }

            System.out.print(aChar);
            }
            }
            }


            But, again it needs typecasting.
            for ASCII values check ASCII Table






            share|improve this answer















            You can use ASCII values to change cases. in your case converting lower case alphabets to upper case loweer case a has ASCII value 97 and z has 122 and if these number is exactly subtracted by 32 then it will be equal to ASCII value of its uppercase letter like 97-32=65 is ASCII value of A. Hope this helps.



            public class toUpperCase{
            public static void main(String args){

            toUpperCase(args[0]);
            }

            //for lower to upper case
            public static void toUpperCase(String a){

            for (int i = 0; i< a.length(); i++){

            char aChar = a.charAt(i);
            if (97 <= aChar && aChar<=122){
            aChar = (char)( (aChar - 32) );
            }

            System.out.print(aChar);
            }
            }
            //for upper to lower case
            public static void toUpperCase(String a){

            for (int i = 0; i< a.length(); i++){

            char aChar = a.charAt(i);
            if (65 <= aChar && aChar<= 97){
            aChar = (char)( (aChar + 32) );
            }

            System.out.print(aChar);
            }
            }
            }


            But, again it needs typecasting.
            for ASCII values check ASCII Table







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 15 '18 at 7:02

























            answered Nov 14 '18 at 15:32









            BhandariBhandari

            162213




            162213













            • Check your code, it does not works! aChar goes nowhere

              – oleg.cherednik
              Nov 14 '18 at 15:57











            • Thanks for letting me know we should print aChar

              – Bhandari
              Nov 14 '18 at 16:01



















            • Check your code, it does not works! aChar goes nowhere

              – oleg.cherednik
              Nov 14 '18 at 15:57











            • Thanks for letting me know we should print aChar

              – Bhandari
              Nov 14 '18 at 16:01

















            Check your code, it does not works! aChar goes nowhere

            – oleg.cherednik
            Nov 14 '18 at 15:57





            Check your code, it does not works! aChar goes nowhere

            – oleg.cherednik
            Nov 14 '18 at 15:57













            Thanks for letting me know we should print aChar

            – Bhandari
            Nov 14 '18 at 16:01





            Thanks for letting me know we should print aChar

            – Bhandari
            Nov 14 '18 at 16:01



            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine