Last active
December 19, 2015 09:09
-
-
Save rfaisal/5931032 to your computer and use it in GitHub Desktop.
DancingSentence: TopCoder SRM 279
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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