// Using String builder and String bufferpublicclassMethod1{publicstaticvoidmain(String[]args){// pass the string to reverseStringBuildersb=newStringBuilder("Amritanshu");StringBuffersbf=newStringBuffer("Rawat");// printingSystem.out.println("String Builder: "+sb.reverse().toString());System.out.println("String Buffer: "+sbf.reverse().toString());}}
Method 2 - String Concatenation
// String ConcatenationpublicclassMethod2{publicstaticvoidmain(String[]args){// String to reverseStringname="Amritanshu Dev Rawat";// empty string to store reverse stringStringrev="";// reverse loop logic and appending to revfor(inti=name.length()-1;i>=0;i--){rev+=name.charAt(i);}// printingSystem.out.println(rev);}}
Method 3 - Character Array
// Char arraypublicclassMethod3{publicstaticvoidmain(String[]args){// String to reverseStringname="Amritanshu";// converting to char arraychar[]charName=name.toCharArray();// reverse loop logic and printingfor(inti=charName.length-1;i>=0;i--){System.out.print(name.charAt(i));}}}
Method 4 - Stack
// Stackimportjava.util.Stack;publicclassMethod4{publicstaticvoidmain(String[]args){// string to reverseStringname="Amritanshu";// empty character stackStack<Character>charStack=newStack<Character>();// push every character to stackchar[]ch=name.toCharArray();for(inti=0;i<name.length();i++){charStack.push(ch[i]);}// pop element and storeintk=0;while(!charStack.isEmpty()){ch[k++]=charStack.pop();}// convert char to string and printSystem.out.println(String.copyValueOf(ch));}}
Method 5 - Swap
// Two Pointer swappublicclassMethod5{publicstaticvoidmain(String[]args){// String to reverseStringname="Amritanshu Dev Rawat";// converting to char arraychar[]c=name.toCharArray();// one pointer at beginintbegin=0;// one at endintend=c.length-1;// one to store value and swapchartemp;// looping and swappingwhile(end>begin){temp=c[begin];c[begin]=c[end];c[end]=temp;end--;begin++;}//printingSystem.out.println(String.valueOf(c));}}
Accessibility Specialist. I focus on ensuring content created, events held and company assets are as accessible as possible, for as many people as possible.
Also you could have at least used JavaScript if you were going to use the JavaScript tag, may I suggest you remove the JS tag from the post as beginners may be really confused and even experienced developers would probably have a few head scratches over trying to convert things like Stack<Character> charStack = new Stack<Character>(); to JS (I mean I can't even work out what the equivalent would be as I don't know that utility in Java)!
I am a frontend developer focused in creating application with React, Vue and Svelte. Currently, I am a software engineering student at Kasetsart University.
Top comments (5)
Talk about over complicating things (for JavaScript at least)
Job done!
Also you could have at least used JavaScript if you were going to use the JavaScript tag, may I suggest you remove the JS tag from the post as beginners may be really confused and even experienced developers would probably have a few head scratches over trying to convert things like
Stack<Character> charStack = new Stack<Character>();
to JS (I mean I can't even work out what the equivalent would be as I don't know that utility in Java)!I am removing that :)
Wait, it is not JavaScript. I think you put wrong tag.
But it may help JS developers also, as only syntax change :)
You could implement this in almost every language.