Este artículo analiza diferentes formas de invertir una string en Java con ejemplos.
Ejemplos:
Java
// java program to reverse a word import java.io.*; import java.util.Scanner; class GFG { public static void main (String[] args) { String str= "Geeks", nstr=""; char ch; System.out.print("Original word: "); System.out.println("Geeks"); //Example word for (int i=0; i<str.length(); i++) { ch= str.charAt(i); //extracts each character nstr= ch+nstr; //adds each character in front of the existing string } System.out.println("Reversed word: "+ nstr); } } //Contributed by Tiyasa
Java
// Java program to ReverseString using ByteArray. import java.lang.*; import java.io.*; import java.util.*; // Class of ReverseString class ReverseString { public static void main(String[] args) { String input = "GeeksforGeeks"; // getBytes() method to convert string // into bytes[]. byte[] strAsByteArray = input.getBytes(); byte[] result = new byte[strAsByteArray.length]; // Store result in reverse order into the // result byte[] for (int i = 0; i < strAsByteArray.length; i++) result[i] = strAsByteArray[strAsByteArray.length - i - 1]; System.out.println(new String(result)); } }
Java
// Java program to ReverseString using StringBuilder import java.lang.*; import java.io.*; import java.util.*; // Class of ReverseString class ReverseString { public static void main(String[] args) { String input = "Geeks for Geeks"; StringBuilder input1 = new StringBuilder(); // append a string into StringBuilder input1 input1.append(input); // reverse StringBuilder input1 input1.reverse(); // print reversed String System.out.println(input1); } }
Java
// Java program to Reverse a String by // converting string to characters one // by one import java.lang.*; import java.io.*; import java.util.*; // Class of ReverseString class ReverseString { public static void main(String[] args) { String input = "GeeksForGeeks"; // convert String to character array // by using toCharArray char[] try1 = input.toCharArray(); for (int i = try1.length - 1; i >= 0; i--) System.out.print(try1[i]); } }
Java
// Java program to Reverse a String using swapping // of variables import java.lang.*; import java.io.*; import java.util.*; // Class of ReverseString class ReverseString { public static void main(String[] args) { String input = "Geeks For Geeks"; char[] temparray = input.toCharArray(); int left, right = 0; right = temparray.length - 1; for (left = 0; left < right; left++, right--) { // Swap values of left and right char temp = temparray[left]; temparray[left] = temparray[right]; temparray[right] = temp; } for (char c : temparray) System.out.print(c); System.out.println(); } }
Java
// Java program to Reverse a String using ListIterator import java.lang.*; import java.io.*; import java.util.*; // Class of ReverseString class ReverseString { public static void main(String[] args) { String input = "Geeks For Geeks"; char[] hello = input.toCharArray(); List<Character> trial1 = new ArrayList<>(); for (char c : hello) trial1.add(c); Collections.reverse(trial1); ListIterator li = trial1.listIterator(); while (li.hasNext()) System.out.print(li.next()); } }
Java
// Java program to demonstrate conversion from // String to StringBuffer and reverse of string import java.lang.*; import java.io.*; import java.util.*; public class Test { public static void main(String[] args) { String str = "Geeks"; // conversion from String object to StringBuffer StringBuffer sbr = new StringBuffer(str); // To reverse the string sbr.reverse(); System.out.println(sbr); } }
Java
/*package whatever //do not write package name here */ import java.io.*; import java.util.Scanner; class GFG { public static void main (String[] args) { Scanner scanner = new Scanner(System.in); String Str = scanner.nextLine(); char[] arr = Str.toCharArray(); String rev = ""; for(int i = Str.length() - 1; i >= 0; i--) { rev = rev + Str.charAt(i); } System.out.println(rev); } }
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA