+ 1
Extra-Terrestrials task problem
I have the same output as the expected output but I can't complete the test. https://code.sololearn.com/c2eNdpku85hR/?ref=app
5 ответов
+ 2
The problem is you starting to add characters beginning at size(), while the last character of the string is located at size() - 1, since strings are null-indexed, just like arrays.
+ 2
Here you go. Done in Java.
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String word = input.nextLine();
for(int x=word.length()-1;x>=0;x--){
System.out.println(word.charAt(x));
}
}
}
+ 2
import java.util.Scanner;
public class Program
{
static String reverseWords(String word){
String reverseWord="";
for(int i=word.length()-1;i>=0;i--){
reverseWord+=word.charAt(i);
}
return reverseWord;
}
public static void main(String[] args) {
Scanner word=new Scanner(System.in);
String reverseWord=reverseWords(word.next ());
System.out.println(reverseWord);
}
}
0
It's strange. The output was ok.
0
Sure, the output might seem okay, but they are likely comparing their solution to yours, which means the strings are being compared, and since your string would be longer by one character, even if not visible in the output, the comparison would fail, since they are compared first by length and then lexographically.