-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseCstyleString.java
More file actions
62 lines (61 loc) · 2.11 KB
/
ReverseCstyleString.java
File metadata and controls
62 lines (61 loc) · 2.11 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
package reversecstylestring;
import java.util.Scanner;
/**
* @author JOSEPH CONQUEST
* This object after compilation will allow a user to enter a string into
* command prompt. The string is then converted to a char array, reversed, and
* printed to the command prompt
* Note: String input is limited to 100 characters
*/
public class ReverseCstyleString {
/**
* @param args the command line arguments are not used
*/
public static void main(String[] args) {
char[] beforeR = new char[100];
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
if(input.length() < 100){
int i;
for(i = 0; i < input.length(); i++){
beforeR[i] = input.charAt(i);
}
beforeR[i] = '\0';
System.out.print("input was : ");
int j = 0;
while(beforeR[j] != '\0'){
System.out.print(beforeR[j]);
j++;
}
reverse(beforeR);
System.out.print("\nString after reversal: ");
j = 0;
while(beforeR[j] != '\0'){
System.out.print(beforeR[j]);
j++;
}
}
else System.out.println("Error: This class can only take String input"
+ "of 100 character length or less");
}
/*
* The reverse method takes a char[] named str as a parameter and reverses
* the content of str by swapping the first and last characters in the array
* While this is O(n), it will run more efficiently then iterating through
* the whole C-string and copying the contents in reverse. Swapping allows
* for the theoretical O(n) to execute closer to o(n/2)
*/
public static void reverse(char[] str){
int start = 0;
int end = 0;
while(str[end] != '\0') end++;
end--;
while(start < end){
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
}