Reverse Strings in JAVA | (simple & easy)
HTML-код
- Опубликовано: 11 сен 2024
- Reverse Strings in JAVA | (simple & easy)
In today's video, I will be showing you how to reverse a string in Java!
► Software Used:
● IntelliJ IDEA: www.jetbrains....
► Helpful Learning Sites:
● Codecademy: www.codecademy...
● freeCodeCamp: www.freecodeca...
► Song Used:
● One More Time: • [FREE] Lofi Type Beat ...
► Support or Follow me:
● Join our Discord: / discord
● GitHub: github.com/Min...
Very simple & sweet code, thank man!
Glad it helped!
great video man probably shortest programming video i have watched on youtube. i am new to coding how do i excel in this
just keep coding :) if u dont know what to code go learn something new and maybe some cool ideas will pop into ur head!
short and simple, thank you
You're welcome!
If anyone has any other things related to CS or JAVA they want a video on, just reply here!
Join the discord! -> discord.gg/hhGu6mV5wm
Nice short explanation 😊
Glad it was helpful!
Nice work ❤
I think i found an easier way:
String s = "123456789";
StringBuilder x = new StringBuilder(s);
x.reverse();
String output = x.toString();
System.out.println(output);
Your way works and is more simple than my way, however this video is supposed to teach those who don’t know how to reverse a sting WITHOUT StringBuilder. (For example in a AP CSA class they would ask you to do this without StringBuilder)
interviews tend to not allow things like StringBuilder, while yes it is more efficient, they want to see if you know the fundamental logic
great very simple and very understandable thank you thank you
Glad it was helpful!
Dude can u explain why decremented the str.length by 1
Using str.length gives you the length of a string but since getting a char from a string starts at 0 not 1, we need to go back one to get the last letter. For example if we had the string “Hello”, and we got the length it would be 5 but if we got the 5th letter AND start counting from 0 not 1 then we we would have an issue because we would go 0, 1, 2, 3, 4, 5 and if you look closely I just listed 6 numbers. So really using the length you are getting the 6th letter here of a 5 letter word. That’s why we subtract one.
@@mintype thx understood
Simple and easy 👏
Thanks a lot 😊
0:24 greater than or equal to
oh yea thanks
thanks man!!
yw
nice
Thanks
type mismatch bro
im not exactly sure i understand why we do .length - 1; what is the -1 for?
-1 means the index of last character, because index starts from zero, so when you want to get last characters' index you should add this -1
array {1, 2, 3, 4, 5}
There's 5 elements inside, it has a length of 5.
However, if we want to know the index of each element inside the array the value '1' is at position '0' which means element '5' is at position '4', so to get the value '5' we need to write array[4]. given N is the length of the array, n-1 determins the position of those elements.
If simple wrote i = str.length; you would get an out of bounds exception element because the element '5' is at position '4', not position '5'. Following the formular n-1, we say str.-length-1 to ensure we always end up at the last element contained within the array.
Thank you for your valuable information
@@DecartesGreatStar You're amazing!
I keep getting an exception out of bounds error and I’m not sure why.
send ur code here
Why you use that + sign with result?
the plus makes it so the thing after gets ADDED to the string. so the string starts off with nothing in it and each time it goes through the for loop, one letter from the str string is added to result. (it goes backwards so its reversed.)
how do u reverse the string itself
you can set the original string variable's value to the result string variable's value like so:
str = result;
@@mintype what if you wanted to change the string itself inside the loop rather than creating a empty string to store the reversed version on. (thxs for the reply btw, i'm new so that helped)
@@pradipacharya3168 I've done some research and if you wanted to change the string itself inside the loop rather than create an empty string to store the reversed version to, that wouldn't really work. However, this video is to show people how to reverse strings using a for loop, like how they may expect a student to in a computer science class. If you really wanted to reverse a string in less lines of code etc., you could do the following:
String originalString = "Hello, world!";
StringBuilder reversedString = new StringBuilder(originalString).reverse();
System.out.println(reversedString); // prints "!dlrow ,olleH"
@@mintype thank you man, that helped a lot!
glad it helped!
Thank you
yw
Nice
Thanks
StringBuilder
StringBuilder is another way you could do this. However, I am teaching how to do this manually, because in schools, teachers don't want you to take shortcuts.
my results came out diffrent
Can you provide some info? Like give me your code so I can see whats wrong. Also maybe tell me your version of Java? New Java versions come out all the time and can break old code.
discord help server: discord.gg/hhGu6mV5wm
WRONG CODE
??? What went wrong?
@@mintype o
ol
oll
olle
olleh
this is the actual output what i got
Isn’t that hello backwards though? “olleh”
Hi, I am having an error. what mistake i have made?
package Practice;
public class HelloReverse {
public static void main(String[] args) {
String input ="Hello world!";
String output=" ";
for(int i=input.length()-1;i>=0;i--);
output+=input.charAt(i);
System.out.println(output);
You have a ; (semicolon) at the end of your for loop!!!!
updated code:
String input ="Hello world!";
String output=" ";
for(int i=input.length()-1;i>=0;i--) output+=input.charAt(i);
System.out.println(output);
@@mintype Thank you so much!
Thank you
Thank you
Thank you
nice
danke
@@mintype dahnke