Skip to content

Commit 72a4e69

Browse files
authored
Merge pull request darpanjbora#96 from RasmusKnothNielsen/add_alternative_reversal
Added an alternative and maybe more simple way to reverse strings
2 parents f999f80 + b0efd2e commit 72a4e69

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

String Reversal/SimpleReversal.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* Alternative way to do the reversal program.
2+
@Author: Rasmus Knoth Nielsen
3+
*/
4+
5+
public class SimpleReversal
6+
{
7+
public String reverse(String input)
8+
{
9+
// Create a StringBuilder to be able to use reverse() method
10+
StringBuilder output = new StringBuilder();
11+
// Add the input to our output
12+
output.append(input);
13+
// Reverse the string
14+
output.reverse();
15+
// Return the StringBuilder, as a string
16+
return "" + output;
17+
}
18+
19+
// Main method to test if the reverse is successful
20+
public static void main(String[] args) {
21+
22+
SimpleReversal simpleReversal = new SimpleReversal();
23+
String string = "Testing string";
24+
System.out.println("String before reversing: " + string);
25+
System.out.println("String after reversing: " + simpleReversal.reverse(string));
26+
}
27+
}

0 commit comments

Comments
 (0)