-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwap.java
More file actions
76 lines (62 loc) · 2.68 KB
/
Copy pathSwap.java
File metadata and controls
76 lines (62 loc) · 2.68 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Code created by Savannah Smalley
Code created on 6/14/21
The purpose of this code is to swap each pair of digits or letters dependant on user input.
*/
//Here I imported the * utility in order to use Scanner
import java.util.*;
//Class start
public class Swap {
//Here is the main
public static void main(String[] args) {
//declaring the Scanner
Scanner console = new Scanner(System.in);
//Prompting user for input
System.out.println("Please input a string to be swapped: ");
//input gets stored in response2
String response2 = console.nextLine();
//Here we call the method that swaps the letters and print the result
System.out.println(swapLetterPairs(response2));
//Here we prompt the user for the digits to be swapped
System.out.println("Please input a number to be swapped: ");
// here we store that answer into response1
int response1 = console.nextInt();
// Here we print the result from the swapDigitPairs method
System.out.println(swapDigitPairs(response1));
}
//Here is the start of the swapLetterPairs method starts here
public static String swapLetterPairs(String m) {
// Here we convert the string to a character array
char[] ch = m.toCharArray();
// This for-loop is where the swapping happens
for (int i = 0; i < ch.length - 1; i += 2) {
// placeholder will hold the value of ch[i] so that
// both digits don't become the same one when swapped
char placeholder = ch[i];
// Here the character at position [i] will be replaced by ch[i+1] (the next value)
ch[i] = ch[i + 1];
ch[i + 1] = placeholder;
}
// Converting the result into a
// string and return
return new String(ch);
}
//Here is the start of swapDigitPairs method
public static int swapDigitPairs(int n) {
int result = 0;
int place = 1;
//while number is greater than 9 (more than 1 digit) the while loop will continue
while (n > 9) {
//This math is where the digits get swapped.
//We isolate the the digits one by one.
result += place * 10 * (n % 10);
//Here we remove the digit we isolated so we can get the next one
n /= 10;
//Here we get that next digit
result += place * (n % 10);
n /= 10;
//Then we stitch all the digits back together
place *= 100;
}
return result + place * n; //here we return the swapped digits :)
}
}