KaiquanMah's picture
Create 11. From end to start
f0745b9 verified
raw
history blame
1.6 kB
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