-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathChpt10Number01.java
More file actions
43 lines (36 loc) · 1.18 KB
/
Copy pathChpt10Number01.java
File metadata and controls
43 lines (36 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Backward String
Page 653, # 1
Write a method that accepts a String object as an argument
and displays its contents backward. For instance, if the
string argument is "gravity" the method should display "ytivarg".
Demonstrate the method in a program that asks the user to input
a string and then passes it to the method.
*/
import java.util.Scanner; // Needed for the Scanner class
public class Chpt10Number01
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in); // Create a Scanner object to read input.
String input; // To hold the input
// Ask the user to input a string
System.out.print("Enter a word: ");
input = keyboard.nextLine();
// Demonstrate the method by passing it the inputted string.
reverse(input);
}
/*
Method: Accepts a String object as an argument
and displays its contents backward.
For instance, if the string argument is "gravity" the method
displays "ytivarg".
*/
private static void reverse(String word)
{
System.out.print("Your word reversed: ");
for (int i = 0; i < word.length(); i++)
System.out.print(word.charAt(word.length() - 1 - i) );
System.out.println();
}
}