0% found this document useful (0 votes)
28 views

Import Public Class Public Static Void New: // One (STR) // System - Out.println (Two (STR) )

This document contains code for reversing a string in Java. It defines three methods - one(), two(), and three() - for reversing a string. The main() method prompts the user for input, and prints the result of reversing the string using the three() method. The three() method iterates through the string backwards, appending each character to a StringBuilder to construct the reversed string.

Uploaded by

dd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Import Public Class Public Static Void New: // One (STR) // System - Out.println (Two (STR) )

This document contains code for reversing a string in Java. It defines three methods - one(), two(), and three() - for reversing a string. The main() method prompts the user for input, and prints the result of reversing the string using the three() method. The three() method iterates through the string backwards, appending each character to a StringBuilder to construct the reversed string.

Uploaded by

dd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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