Home > Computer Programming > Basic for loops

Basic for loops

January 31, 2013 Leave a comment Go to comments

These are the two methods I asked for in the recent quizzes. Study them. Understand why they work. Answer the understanding questions at the end to make sure you do.

TASK:

Write a method strReverse that takes a string argument s and returns s reversed [6 marks]

Example: strReverse(“Hello”) should return “olleH”. Note this is just an example! Your method should reverse ANY string it is given.

Clues:

  • Your method needs to return a string AND take a string as a parameter
  • You need to set up a for loop that runs from the end of s to the beginning of s
  • You need to use charAt to access the different letters of s, for example s.charAt(5) returns sixth character of s
  • You need to build up your new string character by character, ignoring any spaces that you come across

The method is not required to print anything

METHOD:

String strReverse(String s) {
  String newString;
  for(int i = s.length(); i > 0; i--){
    newString = newString + s.charAt(i - 1);
  }
  return newString;
}

UNDERSTANDING:

  • Which variable is the loop counter?
  • Does the loop counter go up or down?
  • What value does it have when newString is returned?
  • What would happen if we put return newString into the for loop (ie up one line)?
  • Why does it say -1 in the call to charAt()?

TASK:

Write a method stripSpaces that takes a string argument s and returns the s with all spaces removed [6 marks]

Example: stripSpaces(“Hello World”) should return “HelloWorld”. Note this is just an example! Your method should remove spaces from ANY string it is given.

Clues:

  • Your method needs to return a string AND take a string as a parameter
  • You need to set up a for loop that runs from beginning of s to the end of s
  • You need to use charAt to access the different letters of s, for example s.charAt(5) returns sixth character of s
  • You need to build up your new string character by character, ignoring any spaces that you come across
  • You can use a.equals(” “) to test for spaces (assuming a is a one character string that you have)

The method is not required to print anything

METHOD:

String stripSpaces(String s){
  String newString;
  for(int i = 0; i < s.length(); i++){
    if (!s.charAt(i).equals(" ")) {
      newString = newString + s.charAt(i);
    }
  }
  return newString;
}

UNDERSTANDING:

  • In which direction does the loop counter go this time?
  • In the for loop, what would happen if < was changed to <= (less than or equal to)?
  • What does the ! mean?
  • How would you say !s.charAt(i).equals(” “) in plain English?
  • What does the method do when it does find a space?
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s