0% found this document useful (0 votes)
46 views4 pages

Essay Program Java

The document contains a Java program that reverses a given string using a stack data structure. It includes a method to reverse the string and a main function that prompts the user for input. The program then outputs the reversed string to the console.

Uploaded by

Andre Kusuma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

Essay Program Java

The document contains a Java program that reverses a given string using a stack data structure. It includes a method to reverse the string and a main function that prompts the user for input. The program then outputs the reversed string to the console.

Uploaded by

Andre Kusuma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Nama : Andre Kusuma Anwar

NIM : 1822496723
Kelas : Pemrograman 3 (SI171C)

Syntak :
Syntak Text :

package Strukturdata;

import java.util.Stack;

import java.util.*;

public class essay {

public static String reverse(String str) {

if (str == null || str.equals(""))

return str;

Stack<Character> stack = new Stack<Character>();

char[] ch = str.toCharArray();

for (int i = 0; i < str.length(); i++)

stack.push(ch[i]);

int k = 0;

while (!stack.isEmpty()) {

ch[k++] = stack.pop();

return String.copyValueOf(ch);

public static void main(String[] args) {

Scanner start = new Scanner(System.in);


String string;

System.out.print("Enter a string : ");

string = start.nextLine();

String str = string;

str = reverse(str);

System.out.println("Reversed: " + str);

}
HASIL :

You might also like