Java

TOPIC 50 – WALKING THROUGH A STRING

 

 

LESSON NOTE

 

 

TEMPLATE TO WALK THROUGH A STRING

 

By “walking through a String”, we mean going through a String one letter at a time.  It is very similar as going through an array one element at a time.

There is a general algorithm that allows us to do this:

for (int x = 0; x < str.length(); x++)

{

   String c = str.substring(x, x+1);

  

   //Deal with letter stored in c

}

 

Above, for each iteration, c will get a different letter of the String str.  For the first pass, c will get the first letter.  For the second pass, c will get the next letter.  And so on…

 

USEFULNESS

 

Problems dealing with walking through a String are interesting and useful because:

    • Their solution can be quite difficult yet very short and elegant.
    • They are problems that often come up in real programming.
    • They are fun!!! (Right?)

 

EXAMPLES

 

Example 1 - Write a program that will get a String from the user and output it one letter at a time.

 

System.out.println(“Enter a word”);

String word = DummiesIO.getString();

 

for (int x = 0; x < word.length(); x++)

{

   String c = word.substring(x, x+1);

 

   System.out.println(c);

}

 

As you can see, we have only added a single statement to our template.  Getting to full understand the template is very important!  This program would be much harder if we didn’t start with the template.


 

Example 2 – Write a program that will get a String from the user and count the number upper case letters in that String.  Results should be outputted to screen.

 

System.out.println(“Enter a word”);

String str = DummiesIO.getString();

 

int count = 0;

for (int x = 0; x < str.length(); x++)

{

   String c = str.substring(x, x+1);

 

   if (c.equals(c.toUpperCase()))  //compare c to itself upperCased

   {

      count++;

   }

}

System.out.println(“Number of upper case letters: “ + count);

 

Again, we have only added a few lines to the template.  The condition is obviously the most challenging part.  It verifies to see if c and the uppercase version of c are the same.  If they are, then c is already in upper case and we therefore increment the value of count.    

 

Example 3 – Write a program that inverts the order of a String.

 

System.out.println(“Enter a word”);

String s = DummiesIO.getString();

 

String invString = “”;

for (int x = 0; x < s.length(); x++)

{

   String c = s.substring(x, x+1);

 

   invString = c + invString;

}

 

System.out.println(invString);

 

Again, we have only added two lines to our template.  Isn’t this great?

 

Lets look at a trace of the above code.  Lets assume that the user entered “hockey”.

                   

Iteration #

Concatenation

InvString value

Before loop

--

“”

1

“h” + “”

“h”

2

“o” + “h”

“oh”

3

“c” + “oh”

coh

4

“k” + “coh

kcoh

5

“e” + “kcoh

ekcoh

6

“y” + “ekcoh

yekcoh

 

It is very important to note that each time invString is given a value, it is a completely new String that is being given.  We are not adding to the previous String.