<p><a href="https://stackoverflow.com/questions/32149/does-anyone-have-a-good-proper-case-algorithm">Using this question</a> as the base is there an alogrithm or coding example to change some text to Pascal or Camel casing.</p>

<p>For example:</p>

<pre><code>mynameisfred
</code></pre>

<p>becomes</p>

<pre><code>Camel: myNameIsFred
Pascal: MyNameIsFred
</code></pre>

[{'answer_id': 32277, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 0, 'selected': False, 'text': '<p>The only way to do that would be to run each section of the word through a dictionary.</p>\n\n<p>"mynameisfred" is just an array of characters, splitting it up into my Name Is Fred means understanding what the joining of each of those characters means.</p>\n\n<p>You could do it easily if your input was separated in some way, e.g. "my name is fred" or "my_name_is_fred".</p>\n'}, {'answer_id': 32429, 'author': 'ljs', 'author_id': 3394, 'author_profile': 'https://Stackoverflow.com/users/3394', 'pm_score': 3, 'selected': True, 'text': '<p>I found a thread with a bunch of Perl guys arguing the toss on this question over at <a href="http://www.perlmonks.org/?node_id=336331" rel="nofollow noreferrer">http://www.perlmonks.org/?node_id=336331</a>.</p>\n\n<p>I hope this isn\'t too much of a non-answer to the question, but I would say you have a bit of a problem in that it would be a very open-ended algorithm which could have a lot of \'misses\' as well as hits. For example, say you inputted:-</p>\n\n<pre><code>camelCase("hithisisatest");\n</code></pre>\n\n<p>The output could be:-</p>\n\n<pre><code>"hiThisIsATest"\n</code></pre>\n\n<p>Or:-</p>\n\n<pre><code>"hitHisIsATest"\n</code></pre>\n\n<p>There\'s no way the algorithm would know which to prefer. You could add some extra code to specify that you\'d prefer more common words, but again misses would occur (Peter Norvig wrote a very small spelling corrector over at <a href="http://norvig.com/spell-correct.html" rel="nofollow noreferrer">http://norvig.com/spell-correct.html</a> which <em>might</em> help algorithm-wise, I wrote a <a href="http://web.archive.org/web/20080930045207/http://www.codegrunt.co.uk/code/cs/spellcorrect/spell-correct.cs" rel="nofollow noreferrer">C# implementation</a> if C#\'s your language).</p>\n\n<p>I\'d agree with Mark and say you\'d be better off having an algorithm that takes a delimited input, i.e. this_is_a_test and converts that. That\'d be simple to implement, i.e. in pseudocode:-</p>\n\n<pre><code>SetPhraseCase(phrase, CamelOrPascal):\n    if no delimiters\n     if camelCase\n      return lowerFirstLetter(phrase)\n     else\n      return capitaliseFirstLetter(phrase)\n    words = splitOnDelimiter(phrase)\n    if camelCase \n      ret = lowerFirstLetter(first word) \n     else\n      ret = capitaliseFirstLetter(first word)\n    for i in 2 to len(words): ret += capitaliseFirstLetter(words[i])\n    return ret\n\ncapitaliseFirstLetter(word):\n    if len(word) &lt;= 1 return upper(word)\n    return upper(word[0]) + word[1..len(word)]\n\nlowerFirstLetter(word):\n    if len(word) &lt;= 1 return lower(word)\n    return lower(word[0]) + word[1..len(word)]\n</code></pre>\n\n<p>You could also replace my capitaliseFirstLetter() function with a proper case algorithm if you so wished.</p>\n\n<p>A C# implementation of the above described algorithm is as follows (complete console program with test harness):-</p>\n\n<pre><code>using System;\n\nclass Program {\n  static void Main(string[] args) {\n\n    var caseAlgorithm = new CaseAlgorithm(\'_\');\n\n    while (true) {\n      string input = Console.ReadLine();\n\n      if (string.IsNullOrEmpty(input)) return;\n\n      Console.WriteLine("Input \'{0}\' in camel case: \'{1}\', pascal case: \'{2}\'",\n        input,\n        caseAlgorithm.SetPhraseCase(input, CaseAlgorithm.CaseMode.CamelCase),\n        caseAlgorithm.SetPhraseCase(input, CaseAlgorithm.CaseMode.PascalCase));\n    }\n  }\n}\n\npublic class CaseAlgorithm {\n\n  public enum CaseMode { PascalCase, CamelCase }\n\n  private char delimiterChar;\n\n  public CaseAlgorithm(char inDelimiterChar) {\n    delimiterChar = inDelimiterChar;\n  }\n\n  public string SetPhraseCase(string phrase, CaseMode caseMode) {\n\n    // You might want to do some sanity checks here like making sure\n    // there\'s no invalid characters, etc.\n\n    if (string.IsNullOrEmpty(phrase)) return phrase;\n\n    // .Split() will simply return a string[] of size 1 if no delimiter present so\n    // no need to explicitly check this.\n    var words = phrase.Split(delimiterChar);\n\n    // Set first word accordingly.\n    string ret = setWordCase(words[0], caseMode);\n\n    // If there are other words, set them all to pascal case.\n    if (words.Length &gt; 1) {\n      for (int i = 1; i &lt; words.Length; ++i)\n        ret += setWordCase(words[i], CaseMode.PascalCase);\n    }\n\n    return ret;\n  }\n\n  private string setWordCase(string word, CaseMode caseMode) {\n    switch (caseMode) {\n      case CaseMode.CamelCase:\n        return lowerFirstLetter(word);\n      case CaseMode.PascalCase:\n        return capitaliseFirstLetter(word);\n      default:\n        throw new NotImplementedException(\n          string.Format("Case mode \'{0}\' is not recognised.", caseMode.ToString()));\n    }\n  }\n\n  private string lowerFirstLetter(string word) {\n    return char.ToLower(word[0]) + word.Substring(1);\n  }\n\n  private string capitaliseFirstLetter(string word) {\n    return char.ToUpper(word[0]) + word.Substring(1);\n  }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/32241', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1075/']