File size: 1,603 Bytes
f0745b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
Write a method

String endToStart(String word)

which takes a string as its parameter. 
The method constructs an inverted version of the string, where the characters of the original string are from end to beginning.




An example of calling the method:
public static void main(String[] args) {
    System.out.println(endToStart("hi"));
    
    String word = "Hellooo";
    String word2 = endToStart(word);
    System.out.println(word2);
}

Program outputs:
ih
ooolleH








import java.util.Random;

public class Test{
    public static void main(String[] args){
        final Random r = new Random();
        
        String[] words = "first second third fourth programming class method java variable".split(" ");
        for (String w: words) {
            System.out.println("Testing with parameter " + w);
            System.out.println(endToStart(w));
            System.out.println("");
        }
    }
    
    public static String endToStart(String word) {
        String result = "";
        int wordLastIdx = word.length() - 1;
        // for each word in the list
        // go from wordLastIdx to 1st idx (0)
        for (int i = wordLastIdx; i >= 0; i--) {
            result += word.charAt(i);
        }
        return result;
    }
}



Testing with parameter first
tsrif

Testing with parameter second
dnoces

Testing with parameter third
driht

Testing with parameter fourth
htruof

Testing with parameter programming
gnimmargorp

Testing with parameter class
ssalc

Testing with parameter method
dohtem

Testing with parameter java
avaj

Testing with parameter variable
elbairav