Hey everyone! I am a pretty new Processing user as we use it in my programming class and was introduced to it then. In our new unit, we have been given a series of practice problem solving questions and I am stumped on this one. Basically, we were asked to write a program that loads a string from a text file and will make it so any capital letters are turned to lower case (without using toLowerCase and toUpperCase), except for capital letters after periods. They should remain capital letters. I have gotten it so all capital letters will go to lower case (except the first one), but I cannot figure out how to get it so that letters after periods are left capital. Can anyone help me out here? I will post my code and also the text used for the assignment below. I’m also a little unsure of what “toggleCase” is doing as this was given to us by the teacher.
The initial version of this program would read in text from a file INPUT.DAT and output the text with the following changes.
Any SINGLE capital letter at the beginning of a word should be left.
All other capital letters are SHOUTING and should be replaced by lowercase. BUT, HOWEVER, the first letter after a PERIOD should be a capital in the output. this holds even if it wasn’t capitalized in the original. NO OTHER changes should be made.
void setup() {
String[] lines = loadStrings("shout.txt"); // The first section here loads the original string that the program will read.
String text = "";
for (int i=0; i<lines.length; i++) {
text = lines[i];
println("ORIGINAL");
println(text);
}
//-----------------------------------
String output = "";
for (int i=0; i<text.length(); i++) { // This section initializes the char "c" and assigns it. It also checks to see if i (the text) is equal to 0 (first letter). If it is, it reads the entire string
// and makes the necessary changes.
char c = text.charAt(i);
if (i==0) {
if (isLower(c)) c = toggleCase(c);
} else {
if (isUpper(c)) c = toggleCase(c);
}
output += c; // "c" becomes output.
}
println(" ");
println("OUTPUT");
println(output); // Prints the output.
}
//------------------------------------------
boolean isUpper(char c) { // Checks to see if any character is uppercase.
if ((int) c >= 65 && (int) c <= 90) {
return true;
}
return false;
}
//------------------------------------------
boolean isLower(char c) { // Checks to see if any character is lowercase.
if ((int) c >=97 && (int) c <= 122) {
return true;
}
return false;
}
boolean isPeriod(char c) { // Checks to see if any character is a period.
if (c == '.') return true;
return false;
}
//------------------------------------------
boolean isSpace(char c) { // Checks to see if any character is a space.
if (c == ' ') return true;
return false;
}
//------------------------------------------
char toggleCase(char a) {
if ((int) a >= 97 && a <= 122) {
a-=32;
} else if ((int) a >= 65 && (int) a <= 90) {
a+=32;
}
return(char)a;
}