Program for converting Alternate characters of a string to Upper Case.n



You can convert a character to upper case using the toUpperCase() method of the character class.

Example

Following program converts alternate characters of a string to Upper Case.

 Live Demo

import java.util.Scanner;
public class UpperCase {
   public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a string :");
      String str = sc.nextLine();
      str = str.toLowerCase();
      char[] ch = str.toCharArray();
      for(int i=0; i<ch.length; i=i+2){
         ch[i] = Character.toUpperCase(ch[i]);
      }
      System.out.println(new String(ch));
   }
}

Output

Enter a string :
hihowareyou
HiHoWaReYoU

Advertisements