Slip No 4 Q A

Q.Write a java program to display alternate character from a given string.[15 M]

import java.util.Scanner;

public class AlternateCharacters {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = sc.nextLine();

        System.out.println("Alternate characters are:");

        // Printing characters at even index positions
        for(int i = 0; i < str.length(); i = i + 2) {
            System.out.print(str.charAt(i) + " ");
        }

        sc.close();
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top