Reverse string | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Reverse string

I/p string : this is sample test O/p string: siht so elpmas tset How to use trim n split?

23rd Jan 2020, 4:54 PM
LearnerLipi
LearnerLipi - avatar
19 Antworten
+ 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);     } }
23rd Jan 2020, 5:45 PM
r8w9
r8w9 - avatar
+ 4
Use the scanner class to take the input from the user. https://code.sololearn.com/cZYVGu36hCqy/?ref=app
23rd Jan 2020, 6:14 PM
Avinesh
Avinesh - avatar
+ 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); } }
20th Dec 2021, 1:35 PM
nitish goud
nitish goud - avatar
+ 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
23rd Jan 2020, 6:19 PM
Ipang
+ 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
23rd Jan 2020, 6:51 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 2
Avinesh didn't spot that. You are correct, thank you :)
23rd Jan 2020, 7:03 PM
r8w9
r8w9 - avatar
+ 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.
25th Jan 2020, 6:45 AM
Seb TheS
Seb TheS - avatar
+ 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]); } }
13th Jun 2022, 6:34 AM
Zoe Piper
Zoe Piper - avatar
+ 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.
23rd Jan 2020, 6:00 PM
Avinesh
Avinesh - avatar
+ 1
Use String Builder Like StringBuilder sb = new StringBuilder("avaj"); Sb. reverse(); //java
24th Jan 2020, 2:04 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
ahmad dawood No, don't strictly link programming and hacking, even though hacking requires programming knowledge.
25th Jan 2020, 4:59 PM
Seb TheS
Seb TheS - avatar
0
Hey thanks all
24th Jan 2020, 7:05 AM
LearnerLipi
LearnerLipi - avatar
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());
25th Jan 2020, 2:00 PM
sree harsha
sree harsha - avatar
0
Is this easy to hack sites?
25th Jan 2020, 4:39 PM
ahmad dawood
ahmad dawood - avatar
0
can anyone help me with real codes for this project im tired and confuse answer o
23rd Jan 2021, 3:39 AM
Robert Sana
Robert Sana - avatar
- 1
Wasn't this task in a codecoach?
23rd Jan 2020, 8:56 PM
Seb TheS
Seb TheS - avatar
- 1
I was stuck with swapping and joining
24th Jan 2020, 7:05 AM
LearnerLipi
LearnerLipi - avatar
- 1
Why we use string args?
25th Jan 2020, 6:40 AM
Akib Raja
- 1
How to reverse a string in c
8th Feb 2021, 12:02 PM
Mohd Saif
Mohd Saif - avatar