CodeGym /Blog Jawa /Acak /Mbalikake String ing Jawa
John Squirrels
tingkat
San Francisco

# Mbalikake String ing Jawa

## Apa iku Reverse String?

"Miwiti maca String wiwit saka karakter pungkasan nganti karakter pisanan. Bingo! iku senarmu sing dibalik."
Mangkene demonstrasi babagan carane bakal katon.
``````
Input String = "X Y Z";
Output String = "Z Y X"

Input String = "1 2 3";
Output String = "3 2 1";

Input String = "I love Java!";
Output String = "!avaJ evol I";

``````

## Kepiye carane mbalikke String ing Jawa?

Ana macem-macem cara kanggo mbalikke String ing Jawa. Nanging, Jawa ora menehi cara mbalikke () kanggo kelas String . Pendekatan naif diwiwiti kanthi nggunakake loop . Prosedur iteratif tradisional. Mengko kita bisa mbangun kanthi nggunakake cara mbalikke () sing ditawakake kelas liyane kaya StringBuilder lan StringBuffer .

### Cara 1 - The Old School Iterative Way

``````
public class StringReversal {

public static String reverseInputString(String myString) {

if (myString == null)
return myString;

String reverseString = "";

for (int i = myString.length() - 1; i >= 0; i--) {

reverseString = reverseString + myString.charAt(i);
}
return reverseString;
}

public static void main(String[] args) {

String myString1 = "X Y Z";
System.out.println("reverse(" + myString1 + ") = " + reverseInputString(myString1));

String myString2 = "1 2 3";
System.out.println("reverse(" + myString2 + ") = " + reverseInputString(myString2));

String myString3 = "I LOVE JAVA!";
System.out.println("reverse(" + myString3 + ") = " + reverseInputString(myString3));

String myString4 = "My favourite place to learn Java is CodeGym.";
System.out.println("reverse(" + myString4 + ") = " + reverseInputString(myString4));

String myString5 = "My name is Lubaina Khan.";
System.out.println("reverse(" + myString5 + ") = " + reverseInputString(myString5));

// Boundary Case to see what happens if a String is null
String myString6 = null;
System.out.println("reverse(" + myString6 + ") = " + reverseInputString(myString6));

// Boundary Case to see what happens if a String is empty
String myString7 = "";
System.out.println("reverse(" + myString7 + ") = " + reverseInputString(myString7));
}
}
``````

#### Output

reverse(XYZ) = ZYX reverse(1 2 3) = 3 2 1 reverse(I LOVE JAVA!) = !AVAJ EVOL I reverse(Panggonan favorit kanggo sinau basa Jawa yaiku CodeGym.) = .myGedoC si avaJ nrael ot ecalp etiruovaf yM mbalikke(Namaku Lubaina Khan.) = .nahK aniabuL si eman yM reverse(null) = null reverse() =

#### Panjelasan

``````
public static String reverseInputString(String myString) { ... }
``````
Cara reverseInputString njupuk String Input sing diarani myString .
``````
if (myString == null)
return myString;
``````
Priksa manawa String input yaiku myString null. Yen ketemu null, bali input minangka iku. Kita ora perlu nerusake. Iki diarani penanganan kasus wates supaya ora ana kesalahan.
``````
String reverseString = "";
``````
Nyatakake String kosong kanggo nyimpen output.
``````
for (int i = myString.length() - 1; i >= 0; i--) {
reverseString = reverseString + myString.charAt(i);
}
``````
Gunakake loop normal . Initialize iterator i saka indeks pungkasan String input . Akses indeks pungkasan string input lan simpen ing string output. Terus mbaleni nganti tekan indeks 0 utawa wiwitan string input.
``````
return reverseString;
``````
Bali maneh reverseString lan gunakake miturut kabutuhan sampeyan.

### Cara 2 - Panganggone Kelas String Builder

Ing Jawa, isi String yen wis diinisialisasi ora bisa diganti. Mula, ora ana cara mbalikke () kasedhiya kanggo String s. Nanging kelas liyane kaya StringBuilder lan StringBuffer ing Jawa ngemot isi sing bisa diganti utawa diganti. Kanggo mbalikke senar tanpa puteran, lan nggunakake dibangun ing StringBuilder utawa StringBuffer kelas ayo kang katon ing conto ing ngisor iki.
``````
public class StringBuilderReversal {

public static void main(String[] args) {

String input1 = "A B C";
StringBuilder inputText1 = new StringBuilder(input1);
System.out.println("reverse(" + inputText1 + ") = " + inputText1.reverse());

String input2 = "0 1 2 2 3 3 3";
StringBuilder inputText2 = new StringBuilder(input2);
System.out.println("reverse(" + inputText2 + ") = " + inputText2.reverse());

String input3 = "Monday";
StringBuilder inputText3 = new StringBuilder(input3);
System.out.println("reverse(" + inputText3 + ") = " + inputText3.reverse());

String input4 = "I love CodeGym!";
StringBuilder inputText4 = new StringBuilder(input4);
System.out.println("reverse(" + inputText4 + ") = " + inputText4.reverse());

// ReverseString using the StringBuilder class
StringBuilder inputText5 = new StringBuilder("Reverse this String using StringBuilder Class.");
System.out.println("reverse(" + inputText5 + ") = " + inputText5.reverse());

// ReverseString using the StringBuffer class
StringBuffer inputText6 = new StringBuffer("Reverse this String using StringBuffer Class.");
System.out.println("reverse(" + inputText6 + ") = " + inputText6.reverse());
}
}
``````

#### Output

mbalikke(ABC) = CBA mbalikke(0 1 2 2 3 3 3) = 3 3 3 2 2 1 0 mbalikke(Senin) = yadnoM mbalikke(Aku seneng CodeGym!) = !myGedoC evol aku mbalikke(Balik String iki nganggo Kelas StringBuilder .) = .ssalC redliuBgnirtS gnisu gnirtS siht esreveR reverse(Balik String iki nggunakake StringBuffer Class.) = .ssalC reffuBgnirtS gnisu gnirtS siht esreveR

#### Panjelasan

``````
String input1 = "A B C";
StringBuilder inputText1 = new StringBuilder(input1);
``````
Kanggo mbalikke String input sampeyan kudu ngowahi menyang StringBuilder . Kanggo iku, pass String input menyang konstruktor StringBuilder .
``````
System.out.println("reverse(" + inputText1 + ") = " + inputText1.reverse());
``````
Sawise ngowahi String dadi StringBuilder , sampeyan bisa mbalikke lan nyithak output.
``````
// ReverseString using the StringBuffer class
StringBuffer inputText6 = new StringBuffer("Reverse this String using StringBuffer Class.");
``````
Utawa, sampeyan bisa ngirim String langsung menyang StringBuilder utawa StringBuffer .

## Kesimpulan

Ing pungkasan kiriman iki, sampeyan kudu ngerti babagan mbalikke Strings lan carane entuk nggunakake kelas StringBuilder lan StringBuffer liyane. Sampeyan bisa milih cara apa wae miturut penak. Bebas bae kanggo mlumpat nalika sampeyan aran macet. Nganti saiki, terus latihan lan terus bersinar.
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION