Lowercase so that letters after periods are left capital

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;
}
1 Like

You can use the String::indexOf() method in order to find all the . dot character’s positions. :eight_pointed_black_star:

Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#indexOf(int,int)

The 1st letter character found after a . dot character should be capitalized. :bulb:

2 Likes

You’ll need to check to see if the previous character to see if it’s a period. (Or chances are you’ll have to go back two characters because chances are it’s a period followed by a space). In your for loop you can get the previous character by doing i-1 (or i-2 for two back). Make sure you don’t check the previous character for the first character because text.charAt(-1) will throw an error.

toggleCase is using ASCII and casting.

1 Like

Another approach is to use Processing’s own split() function in order to break the String all the places the character . dot is found: :wink:

Processing.org/reference/split_.html

Capitalize each 1st letter of each substring; and then join them again as 1 String. :innocent:

4 Likes

GoToLoop, I tried your approach with the split() function but I have not used this function before and I am not quite sure how to go properly go about it. Could you point me in the right direction, or show me how it would look? Thank you very much for your help.

you deal with the lines on a character level,
if you want to check on something what is word related ( like the first char of a word )
can use like

  for (int i=0; i<lines.length; i++) {
    intext = lines[i];
    String[] words = intext.split(" ");
    printArray(words);
//...
3 Likes

Thanks. I will try it