Комментарии:
Thanks for all the great comments: Couple of things:
1. Yes, using character array is an option. You can use that instead of StringBuffer to save from creating a gazillion strings
2. Don't go for recursion unless asked. My suggestion is to mentally evaluate which is the simpler option. Recursion adds a cognitive load to whoever will read code in the future. So, use it only if it makes the solution simpler and more elegant than the iterative approach.
My solution?
package javaapplication21;
import java.util.Scanner;
public class JavaApplication21{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("WORD: ");
String word = scan.nextLine();
StringBuilder sb = new StringBuilder();
System.out.println(sb.append(word).reverse());
}
}
private static String reverseString(String s){
char [] n = new char[s.length()];
int counter = 0;
for (int i = s.length()-1; i > -1 ; i--) {
n[counter++]=s.toCharArray()[i];
}
return new String(n);
}
String str = "Hello World";
char[] strCharArr = str.toCharArray();
for(int i = 0, k= strCharArr.length-1;i != k; i++, k--) {
char temp = strCharArr[i];
strCharArr[i] = strCharArr[k];
strCharArr[k] = temp;
}
System.out.println(String.valueOf(strCharArr));
thank u sir
Ответитьand how to check if they are palindrome foe example Anna and Reversed Anna is the same thing how to check this.. maybe with str1.equals(str2) something like this?
ОтветитьHow are you able to write like pen on paper , on whiteboard which seems to be running on your system ?
ОтветитьStringBuilder.reverse is about 10x faster than String += charAt().
StringBuilder is only a bit faster than StringBuffer on my computer.
StringBuilder.reverse: (200 - 512 - 4041) ns
StringBuffer.reverse: (230 - 566 - 4442) ns
char[] reversing outer to inner: (422 - 1325 - 8646) ns
String += charAt() for end to 0: (1277 - 5667 - 29 834) ns
Create a char[] .
ОтветитьSwap char from 0 to length -1
ОтветитьThanks, it's perfect explanation
ОтветитьThis is crap coding style, in fact bad programming, I have made better than this code see below,
public class Reverse {
public static void main(String[] args)
{
String rt="Hello World";
String result=rever(rt);
System.out.println(result);
}
static String rever(String str) {
StringBuffer obj=new StringBuffer();
for(int i=str.length()-1;i>=0;i--)
{
obj.append(str.charAt(i));
}
return obj.toString();
}
}
Couldnt we also use stack and just print out the reversed string?
ОтветитьGood vid, keep up the good work.
ОтветитьI would use recorgen🤔 much faster
ОтветитьInt[] StringArray = String.toArray(numString);
String revString = new String("");
For(int I = StringArray.length -1, I--, I >=0) {
RevString += ("" + StringArray[i] );
}
Return RevString;
const reverseString = function (string) {
let j = string.length - 1;
let newString = "";
for (let i = string.length; i != 0; --i) {
newString += string.slice(j, i);
--j;
}
console.log(newString);
}
reverseString("Hello friends !");
Back in my college days, all I did with this kind of problem is that I just put the string on an array and call the array in reverse, well I don't know if it's efficient thou, but it works and got some grade for it. lol
ОтветитьI would love to see this series going for solving 50 such questions...
ОтветитьHi Koushik Cold you please add a tutorial to write a program that reverses a sentence. Ex: Hello World to World Hello
ОтветитьYour manual solution is incorrect and it stems from a common misunderstanding of how strings are represented in Java. In Java, Strings are encoded using UTF-16, so you should preserve the relative ordering of UTF-16 surrogate pairs, i.e. this method fails to correctly reverse a string of code points outside of the basic multilingual plane, for example, emoji characters. In Java, the StringBuilder class already accounts for this if you use the reverse() method, so you should compare its output to your manual reverse on a string of emoji characters, or perhaps Egyptian hieroglyphs.
ОтветитьVery nice way to find out , is a String a palindrome
ОтветитьI think as Indian u should also speak hindi,
ОтветитьThis is more practical
package reverse;
public class reversAString {
public static void main(String[] args) {
String a= "hello";
for (int i=a.length()-1; i >= 0; i--) {
char c = a.charAt(i);
System.out.print(c);
}
}
}
It would be great if you could also mention the BigO for the solution. Thanks so much
Ответитьsir can you upload more questions asked on interviews in java please!
thanks!
These interview questions are soo helpful. So helpful that I landed an offer from Amazon! Thank you!! So much.
Ответитьplease continue this series <3 from Bangladesh
ОтветитьDid you check if this is working with upper Unicode characters? I think one would have to use code points. Otherwise emoticons and such would break.
ОтветитьPlease make a Java course
ОтветитьNice explanation. Some other ways I think this could be done is recurssion going from both ends to the center O(n/2). Another really easy way is to just use a stack, it would automatically return anything in it in reverse order once it is emptied.
ОтветитьThanks a lot
ОтветитьExcellent video. Here’s my like 👍🏼
ОтветитьNice code sir👌
ОтветитьYou are not just teacher, but an incredible teacher... Thanks Kaushik...!!!
ОтветитьVery good explanation. I try something different please have a look.
public static void reverseStringBuffer(StringBuffer sb)
{
int beginning = 0;
int end = sb.length()-1;
for(; beginning<end; beginning++, end--)
{
char c = sb.charAt(beginning);
sb.setCharAt(beginning, sb.charAt(end));
sb.setCharAt(end, c);
}
}
Another method using array of char is as follows
private static String reverseStringManually(String str1)
{
char[] charArray = str1.toCharArray();
char newCharArray [] = new char[charArray.length];
for(int i = charArray.length -1 , k = 0; i >= 0 ; i--, k++)
{
newCharArray[k] = charArray[i];
}
return new String(newCharArray);
}
Expected to discuss most efficient approach
Ответитьkeep making these kinds of videos brother :-)
ОтветитьYou're an amazing teacher .❤️
Please continue this series with frequent uploads.
Thank you, Sir!!!!
ОтветитьHi Kaushik, how about making videos on currently hot topics like Kafka, Rabbit MQ, Cassandra. Messaging frameworks and NoSQL databases? That would be really helpful. Thanks!
ОтветитьHi Kaushik, really appreciate your work.
Thank you!!
Expecting more in this series - coding challenges
These are really awesome, thanks for the good videos.
ОтветитьLol which tech company asks String Reverse in coding interview then interviewer is dumb
ОтветитьPlease Please more of these interview questions, I love the way you explain them!!!
ОтветитьAlso possible option is:
private static String reverseString(String str) {
StringBuilder builder = new StringBuilder();
for (int i = str.length(); i > 0; i--) {
builder.append(str.substring(i - 1, i));
}
return builder.toString();
}
Hi Koushik,
Can you do a video on hash map implementation??
return "oso" == new StringBuilder("oso").Reverse().toString()? true : false
ОтветитьThank you very much Koushik sir. you just rock in your teaching style. Please make courses on Core java concepts, Design patterns, Data Structure/Algorithms sir. Java Brains will become one stop for every Java Programmer from the beginner till whatever is the highest level. Thank you so so much sir.
Ответить