Sunday, November 9, 2014

SRM 638 Div 2 250-pt

Problem
http://community.topcoder.com/stat?c=problem_statement&pm=13521&rd=16081

Convert a word in snake case to camel case. This means remove _'s and each letter after the _ needs to be capitalized.
For example: good_morning_world -> goodMorningWorld.

Code
public string toCamelCase(string variableName)
{
StringBuilder sb = new StringBuilder();
bool makeUpper = false;
foreach(char c in variableName)
{
if (c == '_')
makeUpper = true;
else
{
if (makeUpper)
{
makeUpper = false;
sb.Append(Char.ToUpper(c));
}
else
sb.Append(c);
}

}
return sb.ToString();

}

Analysis
Iterate through the string once. It's O(n) where n=length of string. 

No comments:

Post a Comment

There was an error in this gadget