Advertisement
Spocoman

04. Reverse String

Oct 16th, 2024
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String input = scanner.nextLine(),
  7.                 reverseString = "";
  8.  
  9.         for (int i = input.length() - 1; i >= 0; i--){
  10.             reverseString += input.charAt(i);
  11.         }
  12.    
  13.         System.out.println(reverseString);
  14.     }
  15. }
  16.  
  17. Solution with methods:
  18.  
  19. import java.util.Scanner;
  20. import java.io.*;
  21.  
  22. class Main {
  23.     public static void main(String[] args) {
  24.         Scanner scanner = new Scanner(System.in);
  25.         String reverseString = new StringBuilder(scanner.nextLine()).reverse().toString();
  26.    
  27.         System.out.println(reverseString);
  28.     }
  29. }
  30.  
  31. OR:
  32.  
  33. import java.util.Scanner;
  34. import java.io.*;
  35.  
  36. class Main {
  37.     public static void main(String[] args) {
  38.         Scanner scanner = new Scanner(System.in);
  39.  
  40.         System.out.println(new StringBuilder(scanner.nextLine()).reverse().toString());
  41.     }
  42. }
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement