Change case of characters in given string in java


In this core java programming tutorial we will write a program to Change case of characters in given string in java

Write a program that will help us in changing case of characters in String in java.

Example in java>
input String : Java Made So Easy
output         : jAVA mADE sO eASY





Must know >

ASCII value of
A-Z = 65-90 where
A=65, Z=90

and ASCII value of
a-z = 97-122 where
a=97, z=122



Full Program/SourceCode / Example to Change case of characters in given string in java >
/** Copyright (c), AnkitMittal www.JavaMadeSoEasy.com */
class ChangeStringCharactersCaseExample {
   public static void main(String args[]) {
          String inputString = "Java Made So Easy";
          System.out.println("input String : "+inputString);
          System.out.println("output    : "+changecase(inputString));
         
   }
  
   /**
   * method returns changed cases of characters in String.
   */
   static String changecase(String inputString) {
          char ar[]=inputString.toCharArray();
         
          for (int i = 0; i < ar.length; i++) {
                
                 if (ar[i]>=65 && ar[i] <=90) { //convert upperCase to lowerCase
                       ar[i] += 32;
                 } else if(ar[i]>=97 && ar[i] <=122){ //convert lowerCase to upperCase.
                       ar[i] -= 32;
                 }
                
          }
          return new String(ar);
   }
  
}
/*OUTPUT
input String : Java Made So Easy
output    : jAVA mADE sO eASY
*/


So, in this core java programming tutorial we wrote a program to Change case of characters in given string in java


Previous program                                                                  Next program



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>


>Pattern/Pyramid generating program in java




No comments:

Post a Comment