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


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

Updated on: 30-Jul-2019

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements