Debugging Practice

From CompSciWiki
Revision as of 00:58, 6 April 2010 by JonathanG (Talk | contribs)

Jump to: navigation, search

Back to the Program-A-Day homepage

Problem

Find the bugs in the code provided below. Hint: there are three of them.

int lastSpace = 0;
int nextSpace = 0;
String result = "";
String reversedWord = "";
String currWord = "";

for( int i = 0; i < str.len; i++ )
{
  nextSpace = str.indexOf( ' ', lastSpace );
  currWord = str.substring( lastSpace, nextSpace );

  for( int j = 0; j < currWord.len; j++ )
  {
    reversedWord = reversedWord + currWord.charAt( j );
  }

  result = result + reversed;
}
 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

Solution

The solution...

Code

SolutionCode goes here. Please DO NOT put your code in <pre> tags!

Back to the Program-A-Day homepage