CJ_Slip_12A

Slip No.11

A) Write a java program to display each String in reverse order from a String array.

1. Open Notepad.

2. Type the following code:

    public class ReverseStringArray {
    public static void main(String[] args) {
    // Declare and initialize a string array
    String[] words = {"Java", "Hello", "World", "India"};

    // Outer loop: goes through each string in the array
    for (int i = 0; i < words.length; i++) {
    String word = words[i];
    String reversed = "";

    // Inner loop: reverses the current word
    for (int j = word.length() - 1; j >= 0; j--) {
    reversed = reversed + word.charAt(j);
    }

    // Print reversed word
    System.out.println("Original: " + word + " | Reversed: " + reversed);
    }
    }
    }
3. Save the file with the name ReverseStringArray.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 ReverseStringArray.java

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

7. output:
Original: Java | Reversed: avaJ
Original: Hello | Reversed: olleH
Original: World | Reversed: dlroW
Original: India | Reversed: aidnI

No comments:

Post a Comment