I found a thread with a bunch of Perl guys who argued about this question at http://www.perlmonks.org/?node_id=336331 .
I hope this is not too much of an answer to the question, but I would say that you have a small problem in that it will be a very open algorithm that can have many βmissesβ, as well as hits. For example, let's say you entered: -
camelCase("hithisisatest");
The conclusion could be: -
"hiThisIsATest"
Or: -
"hiThisIsATest"
In no way did the algorithm know what to prefer. You could add additional code to indicate that you prefer more common words, but misses will happen again (Peter Norwig wrote a very small spelling corrector at http://norvig.com/spell-correct.html , which can help the algorithm, I wrote a C # implementation if C # is your language).
I agree with Mark and say that you would be better off having an algorithm that accepts a split input, i.e. this_is_a_test and converts it. This would be easy to implement, i.e. in the pseudo-code: -
SetPhraseCase(phrase, CamelOrPascal): if no delimiters if camelCase return lowerFirstLetter(phrase) else return capitaliseFirstLetter(phrase) words = splitOnDelimiter(phrase) if camelCase ret = lowerFirstLetter(first word) else ret = capitaliseFirstLetter(first word) for i in 2 to len(words): ret += capitaliseFirstLetter(words[i]) return ret capitaliseFirstLetter(word): if len(word) <= 1 return upper(word) return upper(word[0]) + word[1..len(word)] lowerFirstLetter(word): if len(word) <= 1 return lower(word) return lower(word[0]) + word[1..len(word)]
You can also replace my capitaliseFirstLetter () function with the correct case algorithm if you wish.
The C # implementation of the above algorithm is as follows (full console program with a test harness): -
using System; class Program { static void Main(string[] args) { var caseAlgorithm = new CaseAlgorithm('_'); while (true) { string input = Console.ReadLine(); if (string.IsNullOrEmpty(input)) return; Console.WriteLine("Input '{0}' in camel case: '{1}', pascal case: '{2}'", input, caseAlgorithm.SetPhraseCase(input, CaseAlgorithm.CaseMode.CamelCase), caseAlgorithm.SetPhraseCase(input, CaseAlgorithm.CaseMode.PascalCase)); } } } public class CaseAlgorithm { public enum CaseMode { PascalCase, CamelCase } private char delimiterChar; public CaseAlgorithm(char inDelimiterChar) { delimiterChar = inDelimiterChar; } public string SetPhraseCase(string phrase, CaseMode caseMode) {