0
Reverse string
I/p string : this is sample test O/p string: siht so elpmas tset How to use trim n split?
19 Answers
+ 8
import java.util.Scanner;
public class ReverseString
{
public static void main(String[] args)
{
System.out.println("Enter string to reverse:");
Scanner read = new Scanner(System.in);
String str = read.nextLine();
String reverse = "";
for(int i = str.length() - 1; i >= 0; i--)
{
reverse = reverse + str.charAt(i);
}
System.out.println("Reversed string is:");
System.out.println(reverse);
}
}
+ 4
Use the scanner class to take the input from the user.
https://code.sololearn.com/cZYVGu36hCqy/?ref=app
+ 3
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();
String res ="";
for (int x=arr.length-1;x>=0;x--){
res+=arr[x];
}System.out.println(res);
}
}
+ 2
- Split the string into a string array
- Implement a reverse method which creates a char array from the string argument. In the method, swap character of the string, one from each ends gradually to the middle.
-The reverse method returns new string built from the reversed char array.
- Using a loop, pass each element of the string array (from original string) into the reverse method, and let the return value be the element's new value.
- Join the array elements using String::join method to obtain a flipped string (sentence).
https://code.sololearn.com/cdxS7OK5w8Sw/?ref=app
+ 2
LearnerLipi you can use StringBuffer for inbuilt reverse () function.
StringBuffer present java.lang package. StringBuffer object are mutable that means you can perform any changes in existing object.
https://code.sololearn.com/cx57Cbe9Bhx5/?ref=app
+ 2
Avinesh didn't spot that. You are correct, thank you :)
+ 2
Akib Raja When you run Java programs on command prompt you can put some arguments that will be automatically passed to the string array.
+ 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();
// Begin the loop from the last letter in the array; End at the 0 position; step one back.
for (int i= arr.length-1; i>=0; i--)
// print the array
System.out.print(arr[i]);
}
}
+ 1
r8w9 what you have written will reverse the whole sentence. The user wants the words in the sentence to be reversed without the change in the position of the word but only the letters.
+ 1
Use String Builder
Like StringBuilder sb = new StringBuilder("avaj");
Sb. reverse();
//java
+ 1
ahmad dawood No, don't strictly link programming and hacking, even though hacking requires programming knowledge.
0
Hey thanks all
0
Scanner s=new Scanner(System.in);
String st=s.next();
StringBuffer sb=new StringBuffer(st);
System.out.println(“The reverse of “ + st + “ is “ + sb.reverse());
0
Is this easy to hack sites?
0
can anyone help me with real codes for this project im tired and confuse
answer o
- 1
Wasn't this task in a codecoach?
- 1
I was stuck with swapping and joining
- 1
Why we use string args?
- 1
How to reverse a string in c