+ 4

The tests come out wrong although the outcomes are the same

Extra-Terrestrials +10 XP You meet a group of aliens, and their language is just like English except that they say every word backwards. How will you learn to communicate with them? Task: Take a word in English that you would like to say, and turn it into language that these aliens will understand. Input Format: A string of a word in English. Output Format: A string of the reversed word that represents the original word translated into alien language. Sample Input: howdy Sample Output: ydwoh #include <iostream> using namespace std; int main() { string word, b; cin >> word; for(int l =word.length(); l != -1;l--) {b += word[l];} cout << b; return 0; }

26th Mar 2020, 1:17 AM
VagSoft
VagSoft - avatar
4 Réponses
+ 1
CapVag in for loop int l=word.length() refers to null character . Change this to int l=word.length()-1
26th Mar 2020, 3:44 AM
Nandan
Nandan - avatar
+ 4
Try this one: import java.util.*; class ReverseString { public static void main(String args[]) { String original, reverse = ""; Scanner in = new Scanner(System.in); // System.out.println("garbage"); original = in.nextLine(); int length = original.length(); for (int i = length - 1 ; i >= 0 ; i--) reverse = reverse + original.charAt(i); System.out.println("" + reverse); } }
1st Oct 2020, 3:36 AM
MAYURI BAVISKAR
MAYURI BAVISKAR - avatar
- 1
Ty both. They worked like a charm. :)
26th Mar 2020, 11:18 AM
VagSoft
VagSoft - avatar