Extra-Terrestrials task problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

20th Dec 2019, 9:47 AM
Илья Карпенко UA
Илья Карпенко UA - avatar
5 Answers
+ 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.
20th Dec 2019, 9:56 AM
Shadow
Shadow - avatar
+ 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)); } } }
21st Apr 2021, 10:37 AM
Joe Hutchens
Joe Hutchens - avatar
+ 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); } }
7th Oct 2022, 8:05 PM
Ionut Piturlea
Ionut Piturlea - avatar
0
It's strange. The output was ok.
20th Dec 2019, 10:08 AM
Илья Карпенко UA
Илья Карпенко UA - avatar
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.
20th Dec 2019, 10:11 AM
Shadow
Shadow - avatar