Reverse String in Java Practice Program #41

preview_player
Показать описание

This tutorial will walk you through the reverse string in java program! ✅This is a fun little java program that might help you learn java :)

Each "letter" or symbol in a string is called a character. To reverse a string, all you have to do, is store each character, and then stick em backwards!

So:

"dog"

turns into:

Characters 'd' 'o' 'g'

which we can reverse:

'g' 'o' 'd'

and make the reversed string:

"god"

This reverse string in java program can be tricky at first... But SURELY you'll get it :) If you followed along, congrats! You learned-by-doing!

I hope you enjoyed this java program to reverse a string in java! I like to have a nice mix of tutorials and actual java projects for you all :)

Was this reverse string in java program helpful for you? -

Disclosure: The Springboard link provided is linked to my affiliate account & supports the channel.

~

Alex Lee
Рекомендации по теме
Комментарии
Автор

Really enjoyed working through this practice program ... will be working through the rest of your practice programs and tutorials! Please continue uploading programs and tutorials - they are invaluable. As always, thanks loads Alex!

vishy
Автор

This channel is keeping me sane as I study for my Java Final next Monday!! Super underrated channel, cant wait for the blow-up!!

jorgerivera
Автор

Here's by far the simplest solution I've found so far (with user input):

Scanner scan = new Scanner (System.in);
System.out.println("Enter a String: ");
String s = scan.nextLine();

System.out.println(new

marko
Автор

I found an easier way!

public String reverseString(String s) {
String reversedString = "";
for (int i = s.length()-1; i >= 0; i--)
reversedString += s.charAt(i);
s = reversedString;
return s;
}

Basically, you take a string as a parameter.
Then you create an empty string. We'll need it later.
Then, you start a loop which basically says to copy each character of "s" starting from the left to this empty string.
then set "s" to the reversedString and return.

qwerasdfhjkio
Автор

Alex you know, I am beginner of java but after listen your video my enthusiasm is build up. I am really apricated by heart to see your amazing job

kamalkaur
Автор

That was a great tutorial. I didn't expect that you'd explain how would the computer read the code. That made me easier to compose in my mind all the codes that you've written. Thank you so much.

anathadenver
Автор

A more efficient way of coding this is to just make it one for loop to decrement through the initial String but making a int tempIndex starting at 0, counting through the decrements, to add each char to the array using the string constructor to pass the now reversed char array to a string.

ClarkieReidri
Автор

You are great Alex, Really njoyed working with you... I can easily understand all your concepts ... It's supper fun Thank you ❤️

mamtasingha
Автор

Amazing and crystal clear explanation. Really enjoyed this practice program. Thank you Alex

imtiazbasha
Автор

Used this week 2 day 2 of Revature Java training. Very helpful. Thanks!

DC-xjfe
Автор

omg i was finally able to run my first program after watching this video! this helped me so much!

AngieGiovanna
Автор

Hey Alex I have recently joined your discord server and wondered if u are available anytime as I have some questions as I have just started coding. Love your content by the way! Much easier to learn Java with you than any channel I have found before.

crazywarrior
Автор

Thank you very much for this video! I am studying applied informatics and I’m in the first semester. During corona everything‘s a little complicated, so I’m even more glad that you uploaded this tutorial. Have a good one! ☺️

marinastr
Автор

I think this code is a lot easier, just so we don't have to use arrays:

public class ReverseString {
public static void main(String[] args) {
String test = "dog"; // have the string "dog"
reverse(test); // reverses and prints out dog
}
public static void reverse(String s){
StringBuilder result = new StringBuilder(); // creating a new string called result

for (int h = s.length() - 1; h >= 0; h--) { // go through the s backwards
result.append(s.charAt(h)); // appends or add the characters into our new string result
}
System.out.println(result); // print out the reverse version
}
}

ItzKingzz
Автор

public static void main(String[] arg){
String myStr = "hello";
String h ="";
for(int i = myStr.length() - 1; i >= 0; i--){
h = h + myStr.charAt(i);
}

System.out.println(h);

}


Wouldnt this be easier?

searemebrahtu
Автор

if java can make dog to god, imagine what java could do to you

werren
Автор

I gotta admit, learning with this channel is much more fun, keep it up :D

linusziegler
Автор

i think u are an angel. u are amazing. i learn how helpful humans are from ur kind work.

flashinstincts
Автор

made this code to have ask for input --- works charm

indranildutta
Автор

{
String s="Dog", rev="";
Int l=s.length();
For(i=l-1;1>=0;1--)
{
rev=rev+s.charAt(i);
}
sysout(rev)
}

mohanrajpalanisamy