Skip to content

Instantly share code, notes, and snippets.

@rfaisal
Last active December 19, 2015 09:09
Show Gist options
  • Save rfaisal/5931032 to your computer and use it in GitHub Desktop.
Save rfaisal/5931032 to your computer and use it in GitHub Desktop.
DancingSentence: TopCoder SRM 279
//Link: https://github.com/rfaisal/hellouniverse/blob/master/Java/src/strings/DancingSentence.java
public static String convert(String s){
StringBuilder sb = new StringBuilder(s);
boolean dancingFlag=true;
for(int i=0;i<sb.length();i++){
if(sb.charAt(i)==' ')
continue;
else if(dancingFlag)
sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
else
sb.setCharAt(i, Character.toLowerCase(sb.charAt(i)));
dancingFlag=!dancingFlag;
}
return sb.toString();
}
//Link: https://github.com/rfaisal/hellouniverse/blob/master/Java/src/unittests/DancingSentenceTest.java
//Topcoder system test passed
@Test
public void testConvert() {
assertEquals("ThIs Is A dAnCiNg SeNtEnCe", DancingSentence.convert("This is a dancing sentence"));
assertEquals(" ThIs Is A dAnCiNg SeNtEnCe ", DancingSentence.convert(" This is a dancing sentence "));
assertEquals("AaAaAaAaAaA", DancingSentence.convert("aaaaaaaaaaa"));
assertEquals("Z", DancingSentence.convert("z"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment