CSU Banner

CS 150, Spring 2018

Lab 10 - For Loops, Strings, and Character Manipulation

Wedensday - March 28th, 2018


Objectives of this Lab

  1. Review the concept and execution of a for loop,
  2. practice writing for loops to manipulate Strings, and
  3. practice using the character primitive type with the Character wrapper class.

Getting Started

Create a new Java Project named Lab10, and make a class named Lab10 with a main method.

Character Wrapper Class

Your TA will quickly review character primitives, the reason for the Character wrapper class, and the underlying ASCII representation for characters.

Understanding For Loops

Consider what each loop will output without first running it in Eclipse. You are certainly welcome to copy and paste this code into Eclipse to verify your expectations for each loop; however, this code and its successive output to the console are not required for today's lab grade. Note that even though most for loops are designed to start at a variable initialized to zero and are incremented after each iteration, it is useful at times to initialize your loop variable to a value other than zero and decrement the loop variable. Can you think of a situation where counting down will work better than counting up? It can also be useful to change your loop variable by a value other than one, for example i += 10 or i -= 2.

Loop One
for(int i = 5; i <= 30; i+=5)
    System.out.println(i);
Loop Two
for (int k = 7; k > 0; k--)
    System.out.println(k);
Loop Three
for (int l = 0; l > 0; l++)
    System.out.println(l);
Loop Four
for (int i = 0; i < myString.length(); i++)
    System.out.print(myString.charAt(i));

Today's Assignment

  1. Declare a String variable called s0 and initialize it to "BASIC,C++,Erlang,Fortran,Python".
  2. Declare a String variable called s0ReverseCase and initialize it to the empty String.
  3. Declare a String variable called s1 and initialize it to the Confucius quote, "It does not matter how slowly you go, so long as you do not stop."
  4. Declare five int variables with the following names: upperCaseCounter, lowerCaseCounter, specialCharCounter, whitespaceCounter, letterOCounter. Initialize all to 0.

  5. 1st For Loop
  6. Your first for loop will count the number of upper case, lower case, and special case characters in s0. To do this you will need to use a few of the Character wrapper class methods. For this loop you will use the isUpperCase() and isLowerCase() methods as well as the String class charAt() method we have seen previously. In this step write a for loop designed to iterate over s0.
  7. Open this link in another tab as a reference for the Character wrapper class methods.
  8. First, write an if statement that handles the occurence of an upper case letter in s0. If there is an upper case letter at the index location, increment upperCaseCounter.
  9. Next, write another conditional that handles the occurence of a lower case letter in s0. If there is a lower case letter at the index location, increment lowerCaseCounter.
  10. Otherwise, increment specialCharCounter.
  11. Here is an example of how to use the Character wrapper class method, isLetterOrDigit(), for a single character from s0 in an if statement:
    if(Character.isLetterOrDigit(s0.charAt(index))) {     
           counter++; 
    }

    2nd For Loop
  12. The second for loop is meant to reverse the case of every letter in s0. For this loop you will again use isUpperCase() and isLowerCase(), but you will also need to incorporate toUpperCase() and toLowerCase().
  13. Start by checking if a character at an index is lower case in s0. If it is, then modify the same character using the toUpperCase() method and append the altered character to s0ReverseCase by concatenating.
  14. Next, write another conditional that handles the occurence of an upper case letter in s0. If so, change the character to lower case using the toLowerCase() method and append the character to s0ReverseCase.
  15. Otherwise the character at a specific index does not meet either of the above conditions, so simply append it unmodified to s0ReverseCase.
  16. Here is example using a Character wrapper class method in an if statement as well as using another Character method in the body of the if statement to modify one character in a String:
    if(Character.isMirrored(s0.charAt(index))) { 
           s0Modified = s0Modified + Character.toTitleCase(s0.charAt(index)); 
    }

  17. Next, outside of the for loop, print the following four lines of output to the console followed by a space and the variable with the appropriate value. Add a blank line after the four lines.
    s0 Upper Case Letters:
    s0 Lower Case Letters: 
    s0 Special Characters: 
    s0 Reverse Case: 

    3rd For Loop
  18. The next for loop will iterate over s1.
  19. First, write an if statement that checks if the current character in s1 is whitespace. If so, increment whitespaceCounter.
  20. Second, write another if statement that uses the boolean comparison operator == to check if the current character in s1 is equal to 'o'. If so, increment letterOCounter.
  21. Next, outside of the for loop, print the following two lines of output to the console followed by a newline, a tab, and then the appropriate variable. Add a blank line after the two lines.
    s1 Number Whitespaces: 
    s1 Number o's: 


Console Output

s0 Upper Case Letters: 9
s0 Lower Case Letters: 16
s0 Special Characters: 6
s0 Reverse Case: basic,c++,eRLANG,fORTRAN,pYTHON

s1 Number Whitespaces: 
	14
s1 Number o's: 
	12 




© 2018 CS150 Colorado State University. All Rights Reserved.
CS Banner
CS Building