We are going to develop a function that will reverse the given
string while preserving the positions of capital letters and
punctuation marks and spaces
For example ---
/* now we are going to create a function that will
reverse the given string whithout touching the
positions of capital letters . and punctuation marks */
import java.util.* ;
public class MINtech{
// Define a static method named "MINtechJAVA" which takes a string as input
public static String MINtechJAVA(String str){
// Convert the input string to a character array
char[] arr = str.toCharArray() ;
// Initialize two pointers, "left" and "right", to reverse the string
int left = 0 ;
int right = arr.length -1 ;
// Start a loop that continues until "left" is less than "right"
while(left < right){
// Move the "left" pointer to the right while it's not a letter
while(left < right && !Character.isLetter(arr[left])){
left++ ;
}
// Move the "right" pointer to the left while it's not a letter
while(left < right && !Character.isLetter(arr[right])){
right -- ;
}
// Swap the characters at the "left" and "right" positions
char temp = arr[left] ;
arr[left] = arr[right] ;
arr[right] = temp ;
// Move the pointers inward
right -- ;
left ++ ;
}
// Convert the character array back to a string and return it
return new String(arr) ;
}
// Main method to execute the program
public static void main(String[] args) {
// Create a Scanner object to read user input
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string that you want to reverse : ");
// Read a line of input from the user
String str = sc.nextLine();
// Call the "MINtechJAVA" function and print the reversed string
System.out.println(MINtechJAVA(str));
}
}
Output
Enter a string that you want to reverse :
MINtech is amzing
gnizmas ih cetNIM
Enter a string that you want to reverse :
I Love MINtech
h cetN IMevoLI
https://www.toprevenuegate.com/rzm38ght?key=d841e7a69b77ba94bcb8e38c0573aab0
No comments:
Post a Comment