+ 3
Reverse string in java
String s="hello"; int length=s.length(); String s1=""; for(int j=1;j<=length; j++) { s1+=s[length-j]; // shows me error here ..array required but string found System.out.println(s1); } how to rectify it?? help me plz
9 Answers
+ 13
String s="hello";
int n=0,length=s.length();
String f,r="";
while(n<length)
{
f=s.Substring(n,n+1);
r=f.concat(r);
n++;
}
System.out.print(r);
//see this âș
+ 12
dude ... s is not string array ... thats the mistake
+ 11
@sahil
replace "Substring" by "substring" ,
by mistake i written the method name initial as capital & java is case sensitive
//it is working now âșđ
+ 6
More effecient than subString:
String myString = "theString";
char[] array = myString.toCharArray();
for(int i = 0; i < array.length / 2; i++){
char temp = array[i];
array[i] = array[array.length - i - 1];
array[array.length - i - 1] = temp;
}
myString = String.valueOf(array);
+ 4
String s = "string";
StringBuilder sb = new StringBuilder(s);
System.out.println(sb.reverse());
+ 3
@Gaurav Agrawal
hii bro....
this code is giving error in line
s.Substring(n,n+1)...
rectify your own self.....
+ 3
@Gaurav ...ohh thnks buddy
+ 2
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
char[] arr = text.toCharArray();
for(int i=arr.length-1;i>=0;i--)
{
System.out.println(arr[i]);
}// Just we start printing from back side
}
}
+ 1
but if I give in string array [0] I'll put hello into them not h