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

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

This Java code defines three methods to reverse a string: one() prints the characters of the input string in reverse order; two() uses the StringBuilder reverse() method to return the reversed string; three() uses a StringBuilder to append the characters of the input string in reverse order and return the result. The main() method prompts the user for a string, and uses three() to print 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)
27 views

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

This Java code defines three methods to reverse a string: one() prints the characters of the input string in reverse order; two() uses the StringBuilder reverse() method to return the reversed string; three() uses a StringBuilder to append the characters of the input string in reverse order and return the result. The main() method prompts the user for a string, and uses three() to print 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

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

Page 1 of 1

You might also like