- 1
debug my code
5 Answers
+ 15
import java.io.*;
import java.util.*;
public class Program
{
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.println("please enter your name :-");
String name = sc.nextLine();
String new_name = "";
//reversing the String using for loop.
for(int i = name.length() - 1; i>=0; i--)
{
new_name += name.charAt(i);
}
System.out.println("reverse String is :-\n "+ new_name);
}
}
+ 5
import java.util.Scanner;
public class Program
{
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
System.out.println("please enter your name :-");
char[] name = sc.nextLine().toCharArray();
String new_name = "";
//reversing the String using for loop.
for(int i=name.length-1;i>=0;i--) {
new_name += name[i];
}
System.out.println("reverse String is :-\n "+ new_name);
}
}
+ 3
I write it in comments bir simply here
1-declarations should be made outside loop
2-after calling a method use () in your case name.length ();
3. for using a string as like as array you need a method as like as length ()
in your case you should use
name.charAt (i);
+ 2
it is much better that share your code from codeplayground by link