-
Notifications
You must be signed in to change notification settings - Fork 0
/
01FirstReverse.java
41 lines (32 loc) · 975 Bytes
/
01FirstReverse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
Problem Description: Using the Java language, have the function FirstReverse(str) take the str parameter
being passed and return the string in reversed order.
*/
import java.util.*;
import java.io.*;
class Function {
String FirstReverse(String str) {
StringBuffer myString = new StringBuffer(str.length());
int length = str.length()-1;
for (int i = 0; i < str.length(); i++) {
myString.append(str.charAt(length));
length--;
}
str = myString.toString();
return str;
}
/**********ALTERNATE SOLUTION*********/
String FirstReverse(String str) {
String myString = "";
for(int i = 0; i < str.length(); i++){
myString = str.charAt(i) + myString;
}
return myString;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
Function c = new Function();
System.out.print(c.FirstReverse(s.nextLine()));
}
}