File tree Expand file tree Collapse file tree 1 file changed +18
-17
lines changed
src/main/java/com/thealgorithms/stacks Expand file tree Collapse file tree 1 file changed +18
-17
lines changed Original file line number Diff line number Diff line change 3
3
import java .util .Stack ;
4
4
5
5
public final class ReverseStringUsingStack {
6
- private ReverseStringUsingStack () {
7
- }
6
+ private ReverseStringUsingStack () {
7
+ }
8
8
9
- /**
10
- * @param str string to be reversed using stack
11
- * @return reversed string
12
- */
13
- public static String reverse (String str ) {
14
- Stack <Character > stack = new Stack <>();
15
- StringBuilder reversedString = new StringBuilder ();
9
+ /**
10
+ * @param str string to be reversed using stack
11
+ * @return reversed string
12
+ */
13
+ public static String reverse (String str ) {
14
+ Stack <Character > stack = new Stack <>();
15
+ StringBuilder reversedString = new StringBuilder ();
16
16
17
- if (str .isEmpty ()) return str ;
18
- for (char i : str .toCharArray ()) {
19
- stack .push (i );
17
+ if (str .isEmpty ())
18
+ return str ;
19
+ for (char i : str .toCharArray ()) {
20
+ stack .push (i );
21
+ }
22
+ while (!stack .isEmpty ()) {
23
+ reversedString .append (stack .pop ());
24
+ }
25
+ return reversedString .toString ();
20
26
}
21
- while (!stack .isEmpty ()) {
22
- reversedString .append (stack .pop ());
23
- }
24
- return reversedString .toString ();
25
- }
26
27
}
You can’t perform that action at this time.
0 commit comments