Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

File - C:\Users\daria\OneDrive\Programming\GitHub\Java\JavaPractice\src\ReverseString.

java
1 import java.util.Scanner;
2
3 public class ReverseString {
4
5     public static void main(String[] args) {
6         Scanner input = new Scanner(System.in);
7         System.out.print("Type a string: ");
8         String str = input.nextLine();
9
10 //        one(str);
11 //        System.out.println(two(str));
12         System.out.println(three(str));
13     }
14
15     public static void one(String str){
16         char[] letters = str.toCharArray();
17         for(int i=str.length()‐1; i>=0; i‐‐){
18             System.out.print(letters[i]);
19         }
20     }
21
22     public static String two(String str){
23         return new StringBuilder(str).reverse().toString();
24     }
25
26     public static String three(String str){
27         StringBuilder sb = new StringBuilder();
28         for(int i=str.length()‐1; i>=0; i‐‐){
29             sb.append(str.charAt(i));
30         }
31         return sb.toString();
32     }
33
34
35
36 }
37

Page 1 of 1

You might also like