-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringFun.java
151 lines (127 loc) · 5.48 KB
/
StringFun.java
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* [StringFun].java
* Author: [Sidney Johnson]
* Submission Date: [3/15/24]
*
* Purpose: To create an environment where the user can enter a
* sentence as a string and then manipulate that string using 5 basic commands.
*
* Statement of Academic Honesty:
*
* The following code represents my own work. I have neither
* received nor given inappropriate assistance. I have not copied
* or modified code from any source other than the course webpage
* or the course textbook. I recognize that any unauthorized
* assistance or plagiarism will be handled in accordance with
* the University of Georgia's Academic Honesty Policy and the
* policies of this course. I recognize that my work is based
* on an assignment created by the School of Computing
* at the University of Georgia. Any publishing or
* posting of source code for this assignment is strictly
* prohibited unless you have written consent from the
* School of Computing at the University of Georgia.
*/
import java.util.Scanner;
public class StringFun {
public static void main(String[] args) {
// creating a new scanner
Scanner keyboard = new Scanner(System.in);
//variable that is true until the user wishes to quit
boolean signal = true;
// asks for string
System.out.println("Enter the string to be manipulated");
String input = keyboard.nextLine();
String message = input;
while (signal) {
// asks for a command
System.out.println("Enter your command (reverse, replace first, replace last, remove all, remove, quit)");
String command = keyboard.nextLine();
// if command isn't valid, error is returned
if (!command.equalsIgnoreCase("quit") && !command.equalsIgnoreCase("reverse") && !command.equalsIgnoreCase("replace first") && !command.equalsIgnoreCase("replace last") && !command.equalsIgnoreCase("remove") && !command.equalsIgnoreCase("remove all")) {
System.out.println("Command invalid. Try again");
}
else {
// ends the program
if (command.equalsIgnoreCase("quit")) {
signal = false;
}
else {
// calls the preverse method, defined below
if (command.equalsIgnoreCase("reverse")) {
System.out.println(reverse(message));
}
// calls the repall method
else if (command.equalsIgnoreCase("replace first")) {
System.out.println("Enter the character to replace");
String blank = keyboard.nextLine();
char oldChar = blank.charAt(0);
// checks if the char input is valid
int count = 0;
for (int i = 0; i < input.length(); i++) {
if (input.charAt(i) == oldChar) {
count += 1;
}
}
if (count > 0) {
System.out.println("Enter the new character");
blank = keyboard.nextLine();
char newChar = blank.charAt(0);
input = repfirst(input, oldChar, newChar);
System.out.println("The new string is: " + input);
}
else {
System.out.println("The letter was not found in the word");
}
}
//remove method
else {
System.out.println("Enter the character to remove");
String blank = keyboard.nextLine();
char oldChar = blank.charAt(0);
input = removall(input, oldChar);
System.out.println("The new string is: " + input);
}
}
}
}
}
// method to reverse
public static String reverse(String message) {
int N = message.length();
String newMessage = "";
for (int i = N-1; i >= 0; i--) {
newMessage += message.charAt(i);
}
message = ("");
message = newMessage;
return message;
}
// method to replace first instance of a character
public static String repfirst(String message, char oldChar, char newChar) {
int N = message.length();
String newMessage = "";
for (int i = 0; i < N; i++) {
if (message.charAt(i) == oldChar) {
newMessage += newChar;
}
else {
newMessage += message.charAt(i);
}
}
return newMessage;
}
// method to remove all instances of a char
public static String removall(String message, char removChar) {
int N = message.length();
String newMessage = "";
for (int i = 0; i < N; i++) {
if (message.charAt(i) == removChar) {
continue;
}
else {
newMessage += message.charAt(i);
}
}
return newMessage;
}
}