Reverse Strings in JAVA | (simple & easy)

  Рет қаралды 93,376

Mintype

Mintype

Күн бұрын

Пікірлер
@mintype
@mintype Жыл бұрын
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
@Spider-Man_67
@Spider-Man_67 Жыл бұрын
Very simple & sweet code, thank man!
@mintype
@mintype Жыл бұрын
Glad it helped!
@ajeetkumarsingh1302
@ajeetkumarsingh1302 9 ай бұрын
great video man probably shortest programming video i have watched on youtube. i am new to coding how do i excel in this
@mintype
@mintype 5 ай бұрын
just keep coding :) if u dont know what to code go learn something new and maybe some cool ideas will pop into ur head!
@User-1234-r1z
@User-1234-r1z 5 ай бұрын
0:24 greater than or equal to
@mintype
@mintype 5 ай бұрын
oh yea thanks
@bankarsandipk
@bankarsandipk Жыл бұрын
Nice short explanation 😊
@mintype
@mintype Жыл бұрын
Glad it was helpful!
@armenia18yt3
@armenia18yt3 6 ай бұрын
short and simple, thank you
@mintype
@mintype 6 ай бұрын
You're welcome!
@Idealbruda
@Idealbruda 4 ай бұрын
Dude can u explain why decremented the str.length by 1
@mintype
@mintype 4 ай бұрын
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.
@Idealbruda
@Idealbruda 4 ай бұрын
@@mintype thx understood
@kevswags
@kevswags Жыл бұрын
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);
@mintype
@mintype Жыл бұрын
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)
@Chilli_Tea
@Chilli_Tea 9 ай бұрын
interviews tend to not allow things like StringBuilder, while yes it is more efficient, they want to see if you know the fundamental logic
@raykhonayorieva7156
@raykhonayorieva7156 Жыл бұрын
great very simple and very understandable thank you thank you
@mintype
@mintype Жыл бұрын
Glad it was helpful!
@renukasp3510
@renukasp3510 Жыл бұрын
Simple and easy 👏
@mintype
@mintype Жыл бұрын
Thanks a lot 😊
@xebex5451
@xebex5451 10 ай бұрын
thanks man!!
@mintype
@mintype 5 ай бұрын
yw
@baseplate_462
@baseplate_462 11 ай бұрын
im not exactly sure i understand why we do .length - 1; what is the -1 for?
@almira4008
@almira4008 10 ай бұрын
-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
@Chilli_Tea
@Chilli_Tea 9 ай бұрын
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.
@ravalimanchala8170
@ravalimanchala8170 8 ай бұрын
Thank you for your valuable information
@frailedtea
@frailedtea 7 ай бұрын
@@Chilli_Tea You're amazing!
@ashishrai472
@ashishrai472 3 ай бұрын
Why we are not using buffer instead of loop
@mintype
@mintype 3 ай бұрын
Because in computer science classes, often times students are asked to do things without buffer. They need to know how to do it without before they use it. (When first learning)
@JSR2428
@JSR2428 8 ай бұрын
Why you use that + sign with result?
@mintype
@mintype 5 ай бұрын
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.)
@chrism2828
@chrism2828 Жыл бұрын
I keep getting an exception out of bounds error and I’m not sure why.
@mintype
@mintype Жыл бұрын
send ur code here
@alejandroquinonescaicedo8
@alejandroquinonescaicedo8 Жыл бұрын
StringBuilder
@mintype
@mintype Жыл бұрын
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.
@pradipacharya3168
@pradipacharya3168 Жыл бұрын
how do u reverse the string itself
@mintype
@mintype Жыл бұрын
you can set the original string variable's value to the result string variable's value like so: str = result;
@pradipacharya3168
@pradipacharya3168 Жыл бұрын
@@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)
@mintype
@mintype Жыл бұрын
@@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"
@pradipacharya3168
@pradipacharya3168 Жыл бұрын
@@mintype thank you man, that helped a lot!
@mintype
@mintype Жыл бұрын
glad it helped!
@sudhabhatnagar5221
@sudhabhatnagar5221 3 ай бұрын
Mere 'Bete' ko lagtaa hai ki 'Strings' se related kuch gaddbad ho gayi aur kuch 'Results' yahaan mil jaayein.
@oshadhaedirisinghe1455
@oshadhaedirisinghe1455 10 ай бұрын
Thank you
@mintype
@mintype 5 ай бұрын
yw
@urboigot45iron4
@urboigot45iron4 2 жыл бұрын
nice
@mintype
@mintype 2 жыл бұрын
Thanks
@code-Known
@code-Known 7 ай бұрын
type mismatch bro
@47lokeshkumar74
@47lokeshkumar74 Жыл бұрын
Nice
@mintype
@mintype Жыл бұрын
Thanks
@tauseefuddeen883
@tauseefuddeen883 19 күн бұрын
String str= "hello wolrd"; String result =" "; For(int i = str. Length() -1; i>=0; i-- ) Result += str.chatAt(i) ;
@amazingtech6726
@amazingtech6726 14 күн бұрын
Result + why this + sign
@sankeerth1088
@sankeerth1088 7 күн бұрын
Str.reverse()
@Lafra-z6z
@Lafra-z6z 3 ай бұрын
You said i less than but you typed greater than sign 😂😂😂
@mintype
@mintype 3 ай бұрын
ops my bad
@irisrin
@irisrin Жыл бұрын
my results came out diffrent
@mintype
@mintype Жыл бұрын
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
@Idealbruda
@Idealbruda 4 ай бұрын
WRONG CODE
@mintype
@mintype 4 ай бұрын
??? What went wrong?
@Idealbruda
@Idealbruda 4 ай бұрын
@@mintype o ol oll olle olleh this is the actual output what i got
@mintype
@mintype 4 ай бұрын
Isn’t that hello backwards though? “olleh”
@Idealbruda
@Idealbruda 3 ай бұрын
@@mintype yes but why it is reversing like this it should only "olleh" not all these
@mintype
@mintype 2 ай бұрын
Likely you put your print statement inside the for loop so it is printing each iteration. The final value of the resulting string after the for loop should be the reverse.
@nahidasultana6842
@nahidasultana6842 Жыл бұрын
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);
@mintype
@mintype Жыл бұрын
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);
@nahidasultana6842
@nahidasultana6842 Жыл бұрын
@@mintype Thank you so much!
@ravalimanchala8170
@ravalimanchala8170 8 ай бұрын
Thank you
@ravalimanchala8170
@ravalimanchala8170 8 ай бұрын
Thank you
@ravalimanchala8170
@ravalimanchala8170 8 ай бұрын
Thank you
@flowersArePretty1
@flowersArePretty1 Жыл бұрын
nice
@mintype
@mintype 5 ай бұрын
danke
@flowersArePretty1
@flowersArePretty1 5 ай бұрын
@@mintype dahnke
Enhanced For Loops in JAVA | (simple & easy)
1:46
Mintype
Рет қаралды 11 М.
My 10 “Clean” Code Principles (Start These Now)
15:12
Conner Ardman
Рет қаралды 315 М.
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
Что-что Мурсдей говорит? 💭 #симбочка #симба #мурсдей
00:19
How to solve any Star Pattern Program
18:47
Simply Coding
Рет қаралды 1,2 МЛН
Reverse Words in a String in Java
4:05
Telusko
Рет қаралды 181 М.
7 Outside The Box Puzzles
12:16
MindYourDecisions
Рет қаралды 172 М.
remove duplicate words in a string | Java by Ponnam Jay |
9:57
Java concepts by Jay tutorial
Рет қаралды 10 М.
Learn JSON in 10 Minutes
12:00
Web Dev Simplified
Рет қаралды 3,3 МЛН
I Helped 2,000 People Walk Again
15:31
MrBeast
Рет қаралды 18 МЛН
Java Program #7 - Reverse a String in Java
4:09
Programming For Beginners
Рет қаралды 15 М.
小丑教训坏蛋 #小丑 #天使 #shorts
00:49
好人小丑
Рет қаралды 54 МЛН