CJ_Slip_4A

Slip No.4

A) Write a java program to display alternate character from a given string.

1. Open Notepad.

2. Type the following code:

    import java.util.Scanner;

    public class AlternateCharacters {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter a string:");
    String input = scanner.nextLine();

    System.out.println("Alternate characters in the given string:");
    for (int i = 0; i < input.length(); i += 2) {
    System.out.print(input.charAt(i) + " ");
    }
    }
    }



3. Save the file with the name AlternateCharacters.java. Make sure to select "All Files" in the "Save as type" dropdown and add the .java extension manually.

4. Open the Command Prompt.

5. Compile the Java program by typing:
javac AlternateCharacters.java

6. Run the compiled Java program by typing:
java AlternateCharacters

7. output:
Enter a string:
Hello World
Alternate characters in the given string:
H l o W r d

No comments:

Post a Comment