Here's a Java program to reverse a given string without using any built-in methods, along with an explanation of how it works:


public class StringReverse {
public static void main(String[] args) {
String originalString = "Hello, World!";
String reversedString = reverseString(originalString);
System.out.println("Original String: " + originalString);
System.out.println("Reversed String: " + reversedString); 
 } 
public static String reverseString(String input) {
// Convert the input string to a character array for easy manipulation
char[] characters = input.toCharArray(); 
// Define two pointers: one at the beginning and one at the end of the character array
int left = 0
int right = characters.length - 1; // Iterate until the two pointers meet at the center of the character array 
while (left < right) { 
// Swap the characters at the left and right pointers 
char temp = characters[left]; 
 characters[left] = characters[right];
 characters[right] = temp; // Move the pointers towards the center for the next iteration
left++;
 right--; }
// Convert the character array back to a string and return the reversed string 
return new String(characters); } 
}


Explanation:

1. The program starts with the main method, where we declare and initialize the originalString variable with the input string that we want to reverse.

2. We then call the reverseString method, passing the originalString as an argument, to obtain the reversed string.

3. In the reverseString method, we first convert the input string into a character array, characters, using the toCharArray method. A character array is mutable, meaning we can modify its elements.

4. We define two pointers, left and right, initialized to the first and last indices of the character array, respectively.

5. Next, we enter a while loop that continues as long as the left pointer is less than the right pointer. This loop is designed to swap characters from the outer ends of the array towards the center until they meet.

6. Inside the loop, we perform character swapping using a temporary variable temp. This technique exchanges the character at the left pointer with the character at the right pointer.

7. After swapping, we increment the left pointer and decrement the right pointer to move them closer to the center of the array.

8. The loop continues until the left and right pointers meet in the middle of the array.

9. Finally, outside the loop, we convert the character array back to a string using the String constructor and return the reversed string.

10. The reversed string is then printed in the main method.


By using this approach, we successfully reverse the given string without utilizing any built-in methods, gaining a deeper understanding of string manipulation in Java.

2. Create a Java method that takes a string as input and returns the number of vowels present in it ?