pages:
- 1
farooq SAYS
Hi
can you help me??
I don`t know what I do!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int num = cin.nextInt();
String str = "";
int i = 0;
while(i <= num){
str = cin.nextLine();
char[] name = str.toCharArray();
int x = name.length;
for(int j = x - 1 ; j >= 0 ; j--){
System.out.print(name[j]);
if(j == 0)
System.out.println();
}
i++;
}
}
}
AMiR SAYS
After pasting your code here, please add 3 backtick characters (`) before and after your code block. This makes your code to be well-formatted.
About your code:
- Start
i
from 1, not zero. - Always, when you want to read the whole line after reading just and integer, put a readline command after reading the integer. Like this:
```
int num = cin.nextInt(); cin.nextLine(); // read the rest of the line after the integer String str = ""; int i = 1;
while(i <= num){
str = cin.nextLine();
char[] name = str.toCharArray();
```
- There might be empty lines in the input, you should print the
\n
for them too. So bringSystem.out.println();
after the loop.
``` import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int num = cin.nextInt();
cin.nextLine();
String str = "";
int i = 1;
while(i <= num){
str = cin.nextLine();
char[] name = str.toCharArray();
int x = name.length;
for(int j = x - 1 ; j >= 0 ; j--){
System.out.print(name[j]);
}
System.out.println();
i++;
}
} } ```
farooq SAYS
thank you so much!
M.Khooryani SAYS
Hi! public static void main(String[] args) { Scanner in = new Scanner(System.in);
int t = in.nextInt();
in.nextLine();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < t; i++) {
sb.append(new StringBuilder(in.nextLine()).reverse()).append("\n");
}
System.out.print(sb);
}
Login please!
In order to post something you must login first. You can use the form in the top of this page.