Reverse String in Java Practice Program



New course coming soon! Learn Java Fast launches on July 1st, 2020. Stay informed and join the waitlist:

– This Java program will reverse a string for you! ✅This is a fun little java program that might help you learn java 🙂

👑Start practicing now with 10 free java programs –
🌅 Source Code –

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 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 🙂

Do you want to learn how to write java code from scratch, in Java? Check out my Java Tutorial For Beginners:

Was this program to reverse a string in java without using an api helpful for you? –

Free tips and programs:

Recommended Channels to Subscribe to
Career Karma:
TheNewBoston:

~

(2nd channel)

Alex Lee

Nguồn: https://infernalaffairsguild.com/

Xem thêm bài viết khác: https://infernalaffairsguild.com/cong-nghe/


Article Categories:
Công Nghệ

Comments

  • Can someone explain what the static does? I thought it just makes stuff constant. Sorry I'm still new to java

    Pikado June 27, 2020 7:12 am Reply
  • you made that harder:
    package com.company;

    import java.util.Scanner;

    public class Game {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    System.out.println("Enter The Word");

    String word = input.next();

    for (int i = (word.length()-1); i >=0; –i)

    System.out.print(word.charAt(i));

    {

    }

    }

    }

    MeeZoid June 27, 2020 7:12 am Reply
  • 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
    }
    }

    xXKillerXx June 27, 2020 7:12 am Reply
  • Every time we concating a new character to the string are we creating new string object?

    Selim Altayev June 27, 2020 7:12 am Reply
  • ☕☕☕

    Sidharth Mishra June 27, 2020 7:12 am Reply
  • I ended up walking away as a better person. Thanks

    CorporateGamer June 27, 2020 7:12 am Reply
  • I want the link for downloading the book,please

    Tarek Rabie June 27, 2020 7:12 am Reply
  • import java.util.*;

    public class Converter {

    public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);

    scan.close();

    String text = scan.nextLine();

    StringBuffer buf = new StringBuffer(text);

    buf.reverse();

    System.out.println(buf);

    }

    }

    Pets Garden 2 June 27, 2020 7:12 am Reply
  • Why do you need an other for loop if this one reverses the string?
    for(int i = str.length() -1 ; i > =0 ; i–) {
    System.out.print( str.charAt(i));
    }

    Irving Purata June 27, 2020 7:12 am Reply
  • you are AWESOME ::)

    James Arden June 27, 2020 7:12 am Reply
  • String myString = "AdemGokceKahramanmaras";

    String myReverseString = new StringBuffer(myString).reverse().toString();

    System.out.println("Here is myString: " + myString);

    System.out.println("Here is myReverseString: " + myReverseString);

    Adem Gokce June 27, 2020 7:12 am Reply
  • I'd like to add, that for those, that are currently working on some other problem and got here by a Google search and are expecting a more out of the box solution for reversing strings, that there is for example the StringBuilder, that offers a .reverse() method:
    String reversed = new StringBuilder("dog").reverse().toString();
    What I'm saying is, that this is a great exercise that shows of how loops can be used in combination with chars. But for those who are new to Java, keep in mind that Java has us covered for many typical problems. Keep watching Alex's great videos though, because they are great for learning!

    Kim Hogeling June 27, 2020 7:12 am Reply
  • Very Clear , Thank you Alex

    Beth Johnathan June 27, 2020 7:12 am Reply
  • I think Alex can be patented as an cartoon character, super viewer friendly

    Beth Johnathan June 27, 2020 7:12 am Reply
  • Thank you very much! this is super helpful ❤️

    Lola Bello June 27, 2020 7:12 am Reply
  • Thank You Alex Lee!😀
    I tried to write it and:
    Without array "letter" like This👇🏻

    public static String reverseString(String string) {
    String reverse = "";
    for (int i = string.length() – 1; i >= 0; i–) {
    reverse += string.charAt(i);
    }
    return reverse;
    }

    Mahdieh ShahHosseinian June 27, 2020 7:12 am Reply
  • Was also hoping you could do another video on this, exactly the same but with an enhanced for loop.

    ferfykins June 27, 2020 7:12 am Reply
  • Love your channel/videos…. Was hoping to get your advice on something… I recently finished a course on java that was 70+ hours worth of video….. So I know the basics of the language.. Wondering where i should go from there… For example… Currently i have no idea where to start with building a project, for example, say a game of tetris, i have no idea where to start on this. Any advice? Thanks!!

    ferfykins June 27, 2020 7:12 am Reply
  • You are a Dog among men my dude!

    Splamoh June 27, 2020 7:12 am Reply
  • Hi Alex, will the letterIndex syntax will work on any online java compilers? because when I am trying to run my program it says symbol cannot be found. Do I need to declare it or that syntax will only work in Eclipse compiler? Please let me know. Thank you in advance.

    Marte Antonio June 27, 2020 7:12 am Reply
  • if java can make dog to god, imagine what java could do to you

    Zein Fadli June 27, 2020 7:12 am Reply
  • Excellent video, can't wait to see more!!

    ferfykins June 27, 2020 7:12 am Reply
  • Why not do this:
    String x = "Hare Krishna";
    StringBuilder sb = new StringBuilder();
    sb.append(x);
    System.out.println(sb.reverse());

    Or this
    String input = "Gopinath";
    char[] c = input.toCharArray();
    for (int i = c.length-1; i>=0; i–)
    System.out.print(c[i]);

    And keep the brain fresh.

    Henry Gen June 27, 2020 7:12 am Reply
  • 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 June 27, 2020 7:12 am Reply
  • I like this java practice. I understand the concept a lot better. Thank you.

    Grace B June 27, 2020 7:12 am Reply
  • thank you sm Alex, i'm a beginner and your videos are really helping me to understand things.

    gülin June 27, 2020 7:12 am Reply
  • thank the lord I found these videos. I'm currently taking a Data Structures and Algorithms course and I am L O S T.

    Jennifer Bijan June 27, 2020 7:12 am Reply
  • Why is it s.length() – 1? What does the minis one do?

    MisterWealth June 27, 2020 7:12 am Reply
  • its a great job. Thanks for the video. Also, I have another way which is time consuming.
    (((((((
    public static void main(String[] args) {

    String s = "Love hates love evol setah evoL";

    String rev ="";

    for(int i = s.length()-1; i>=0 ; i–) {

    rev = rev+ s.charAt(i);

    }

    System.out.println(rev);

    }
    ))))))))

    Galif Chad June 27, 2020 7:12 am Reply
  • i still dont understand wht is the String reverse = ""; for

    Tobiaye June 27, 2020 7:12 am Reply
  • Could’ve just skipped the extra step of making a string and returned the array too right?

    Estevan Prado June 27, 2020 7:12 am Reply
  • Hi bro

    Fun time with Parth and shaurya June 27, 2020 7:12 am Reply

Leave a Comment

Your email address will not be published. Required fields are marked *