[Question]
  [
Some of your employees have broken capslock keys and you're too cheap to replace them. Help them out by creating the shortest program possible to correct their work! Simply convert each character in a given string from uppercase to lowercase and vice-versa... but there's a twist!
You're also very excited for Christmas! So you're going to leave in a tiny "bug" that doesn't correct letters which are within sequences of `Christmas` (case-insensitive).
## Input
For input you will use one single string (or array of bytes) that may contain newlines and ascii between [0x20 and 0x7e](http://www.asciitable.com/) ( - `~`). You don't need to worry about carriage returns or any other characters being in the string.
## Output
The output should contain **only** the provided string with the upper and lowercase characters swapped (and the Christmas bug of course!). It can contain up to one extra trailing whitespace.
## Christmas Bug
Let's explain this with an example:
```
Input: i CAN HARDLY WORK LIKE THIS please GET ME A NEW KEYBOARD FOR cHRISTMAS
Output: I Can HaRdly work lIke thiS PLEASE geT Me A new keyboard for ChriStmas
```
`can` contains "c" which is the first letter of Christmas, so that isn't changed. The next letter in `Christmas` is "h", which is in `hardly` (which also contains the "r"), so that isn't changed, etc. `Christmas` itself only has one letter unchanged because by the time the code reaches there, it's actually looking for "s", not "c".
Once the sequence is found, it should start all over again at "c", and begin iterating through `Christmas` once more. So `ChristmasChristmas` would be left unchanged.
## Test Cases
```
Input: Hello World!
Output: hELLO wORLD!
Input: I like pie :)
Output: i LIKE PIE :)
Input: hELP my KeYboarD
       iS BROKEN
Output: Help MY kEyBOARd
        Is broken
Input: cHRISTMAS IS COMING REALLY SOON!
Output: cHRISTMAS is Coming really soon!
Input: C is the first letter in cHRISTMAS
Output: C IS ThE FIrST LETTER iN ChriSTMAS
```
## Winner
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the shortest answer wins!
  
[Answer]
# [05AB1E](http://github.com/Adriandmen/05AB1E), 16 bytes
Thanks to *Emigna* for saving a byte and fixing a bug!
```
vyÐl'ŒÎ¾èQi¼ëš}?
```
Explanation:
```
vy                # For each character in the string...
  Ð               #   Triplicate that character
   l              #   Convert to lowercase
    'ŒÎ           #   Compressed version of "christmas"
       ¾          #   Push the counting variable (let's call this N)
        è         #   Get the nth character of "christmas", with modular indexing
         Qi   }   #   If equal...
           ¼      #      N += 1
            ë     #   Else...
             š    #      Swapcase
               ?  #   Print that character
```
Uses the **CP-1252** encoding. [Try it online!](http://05ab1e.tryitonline.net/#code=dnnDkGwnxZLDjsK-w6hRacK8w6vFoX0_&input=IiIiaSBDQU4gSEFSRExZIFdPUksgTElLRSBUSElTIApwbGVhc2UgR0VUIE1FIEEgTkVXIEtFWUJPQVJEIEZPUiBjSFJJU1RNQVMiIiI)
[Answer]
# [V](https://github.com/DJMcMayhem/V), ~~38~~, 36 bytes
```
ÄVumaOchristmasòÉf2x`a@"maj~HòHdjV~
```
[Try it online!](https://tio.run/nexus/v#Pc49DoIwAIbh3Su4fPEInkDEahv@kkIgbFYo8o8pGGRhcvE6XIGDIS7ub568y/z2n5VwolRlbVeJdjtP8yfZv67isKtEPtJ5onHuj8uSQddsUI2fzBCBww2YzCDwKHPxKKVoJS7Eg0WgwSYBDBIenbXG2eGIKGeuZ2nuhkEXNajgcTmgb1SBkhUSXZr9mbtcGbkytexRyOHWCBUjaRT0ddP9bX4B "V – TIO Nexus") (contains input and expected output for comparison)
When I first saw this, I thought it would be extremely easy. In fact, if it were not for the "christmas" bug, this would just be 2 bytes: `V~`. The christmas bug makes it significantly harder, for a very hacky answer.
As usual, here is a hexdump:
```
00000000: c456 756d 614f 6368 7269 7374 6d61 731b  .VumaOchristmas.
00000010: f2c9 6632 7860 6140 226d 616a 7e48 f248  ..f2x`a@"maj~H.H
00000020: 646a 567e                                djV~
```
[Answer]
# PHP, ~~113~~ ~~110~~ 102 bytes
```
while($o=ord($c=$argv[1][$i++]))echo chr(32|$o==ord(christmas[$k%9])?$o|0&$k++:ctype_alpha($c)*32^$o);
```
takes input from first command line argument. Run with `-r`.
**breakdown**
```
while($o=ord($c=$argv[1][$i++]))// loop through string characters
    echo chr(
        32|$o==ord(christmas[$k%9]) // if $c equals next character in "christmas"
            ?$o|0&$k++              // no change, increase "christmas" index
            :ctype_alpha($c)        // else if $c is a letter
                    *32^$o          // toggle bit 5 of the ascii code
        );
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~36~~ 30 bytes
```
"@tk'schristma'H)=?HQXHx}Yo]&h
```
Strings with newlines need to be defined by concatenating with the ASCII code `10` (see example in the link with the test cases).
[Try it online!](https://tio.run/nexus/matl#@6/kUJKtXpycUZRZXJKbqO6haWvvERjhUVEbmR@rlvH/v3qmgrOjn4KHY5CLT6RCuH@Qt4KPp7erQoiHZ7BCQU5qYnGqgrtriIKvq4Kjgp9ruIK3a6STP1C1gpt/kEKyR5BncIivY7A6AA "MATL – TIO Nexus") Or [verify all test cases](https://tio.run/nexus/matl#Pc29CsIwGEbh2V7Fq4MfbroK4m8wobXVRtAiBWuNNDS1kmbQwWuvujg@w@Gc297MldTkhdWNqzLig8mU7478@U7qtF@06WrfElfG1DjU1ly75JGA0aXCQyuMB19rBMJn2Ar2c8c7UcGCLaoXfJVc6syuCKMhSEss4shnIaUe5TwWcr@ZSwiJZbQR4RoxmwdBAhlF4e@zhG7gCoWbto2DUc4pC33Hv6UP).
### Explanation
```
"              % Implicit input of a string. For each character in that string
  @            %   Push current character
  tk           %   Duplicate and convert to lowercase
  'schristma'  %   Push string. This is 'Christmas' in lowercase and circularly
               %   shifted such that the 'c' is in the second position
  H            %   Push contents of clipboard H, which is initiallized to 2.
               %   This value will be gradually increased when a new character
               %   from the the sequence is found
  )            %   Get character from 'schristma' at that (modular) position
  =            %   Are they equal?
  ?            %   If so
    HQ         %     Push contents of clipboard H and add 1
    XHx        %     Copy into clipboard K and delete
  }            %   Else
    Yo         %     Change case
  ]            %   End
  &h           %   Concatenate stack contents horizontally. This gives a string 
               %   with all characters processed up to now
               % Implicit end. Implicit display
          
```
[Answer]
## Pyke, ~~31~~ 25 bytes
```
FD.dґ~o@Dl3+{DIoK)R!Il3
```
[Try it here!](http://pyke.catbus.co.uk/?code=FD.d%01%D2%91%7Eo%40Dl3%2B%7BDIoK%29R%21Il3&input=cHRISTMAS+IS+COMING+REALLY+SOON)
[Answer]
# [Perl 6](http://perl6.org/), 84 bytes
```
{my $i=0;[~] (.lc~~"christmas".comb[$i%9]??(++$i&&$_)!!.ord>90??.uc!!.lc for .comb)}
```
[Answer]
# C# 197 bytes
Not going to win with this, but hopefully the smallest C# implementation that works...
```
string C(string s){int i=0,j=0;var r="";for(;i<s.Length;){char c=s[i++],o=(char)32;if(c=="christmas"[j]|c=="CHRISTMAS"[j])j=j>7?0:j+1;else if(c>64&c<91)c+=o;else if(c>96&c<123)c-=o;r+=c;}return r;}
```
Explanation:
```
string C(string s)
{
    // define our two index ints
    // i for indexing across the input string
    // j for indexing across christmas
    int i = 0, j = 0;
    // r is our return string
    var r = "";
    // declare our loop
    // skip the initialisation and afterthought
    for (; i < s.Length;)
    {
        // get our current character c, and increment index i
        // initial our offset char o (difference between upper and lower case)
        char c = s[i++], o = (char)32;
        // check if c is the current character in our christmas bug
        if (c == "christmas"[j] | c == "CHRISTMAS"[j])
            // increment j (or reset to 0)
            j = j > 7 ? 0 : j + 1;
        // else if c is an upper case char
        else if (c > 64 & c < 91)
            // add our offset to make it lower case
            c += o;
        // else if c is lower case
        else if (c > 96 & c < 123)
            // subtract our offset to make it upper case
            c -= o;
        // append c to our return string r
        r += c;
    }
    return r;
}
```
[Answer]
# JavaScript, ~~122~~ ~~118~~ ~~114~~ ~~107~~ ~~104~~ 93 bytes
```
f=
s=>s.replace(/./g,c=>(k=c.toLowerCase())=='christmas'[i%9]?++i&&c:k!=c?k:c.toUpperCase(),i=0)
F=s=>console.log(f(s))
F(`Hello World!`)
F(`I like pie :)`)
F(`hELP my KeYboarD
       iS BROKEN`)
F(`cHRISTMAS IS COMING REALLY SOON!`)
F(`C is the first letter in cHRISTMAS`)
```
* 11 bytes off thanks @Neil.
[Answer]
# [Perl 6](https://perl6.org), 80 bytes
```
{my$i=0;S:g{.?<!{'christmas'.comb[$i%9]eq$/.lc&&++$i}>}=$/eq$/.lc??$/.uc!!$/.lc}
```
[Try it](https://tio.run/nexus/perl6#RY9RTsJAFEX/ZxW3pFIJpvhlIoikwEiblo5pSQhRP8owyMQpxU5rQgh7cBGuzI3ghBj9enkvN@eeV2uBjxuX9wjJ92jyYiXQPx3yvS371720@3pwB3fWweGbUuoqz7Tj8iJfPtny4vZFvNsdV/Fms9225fH@2Lc7v6fBwIyaW9Z5O550tseZ7ep6qatL5/vzy7lqPG8bLayLEkpuhT5JjLwYvpeMowXmLAkRBSHFzA9S7JTIjOuEzjCl8BDTOUK6GDKTxgNLwP0kSGdTLyW@UKrAvCjVyiKBYb8J7KRAt0U2NHqEeTQUi2WRlWOjIVMMExbSmPwRYPpGbBrEEyTUi4xMylhskRGkRrURWMtSV1CiqkQJuf3v/gE "Perl 6 – TIO Nexus")
```
{   # bare block lambda with implicit parameter ｢$_｣
  my $i = 0;             # counter
  S                      # substitute and return ( implicitly against ｢$_｣ )
  :global
  {
    .                    # any char
    ?                    # work around a bug where ｢$/｣ doesn't get set
    <!{                  # fail this match if this block returns True
      'christmas'.comb\  # a list of the characters of ｢christmas｣
      [ $i % 9 ]         # grab a char from the list
      eq                 # is it equal to
      $/.lc              # the lowercase version of the char
      &&                 # if so
      ++$i               # increment ｢$i｣ ( result is True )
    }>
  }
  =                      # for each matched char
  $/ eq $/.lc            # is it lowercase?
  ?? $/.uc               # the uppercase it
  !! $/.lc               # otherwise lowercase it
}
```
[Answer]
# Java 7, 200 bytes
```
String c(char[]a){String r="";int i=0,s;Character l='a';for(char c:a)if((s="christma".indexOf(l=l.toLowerCase(c)))==i|i>7&s==4){r+=c;i=i>7?0:i+1;}else r+=l.isUpperCase(c)?l:l.toUpperCase(c);return r;}
```
Ugly, but it works.. Can definitely without any doubt be golfed more.. I'm rusty..
**Ungolfed:**
```
String c(char[] a){
  String r = "";
  int i = 0,
      s;
  Character l = 'a';
  for(char c : a){
    if((s = "christma".indexOf(l = l.toLowerCase(c))) == i) | i > 7 & s == 4){
      r += c;
      i = i > 7
           ? 0
           : i+1;
    } else{
      r += l.isUpperCase(c)
       ? l
       : l.toUpperCase(c);
    }
  }
  return r;
}
```
**Test code:**
[Try it here.](https://ideone.com/KzZqVL)
```
class M{
  static String c(char[]a){String r="";int i=0,s;Character l='a';for(char c:a)if((s="christma".indexOf(l=l.toLowerCase(c)))==i|i>7&s==4){r+=c;i=i>7?0:i+1;}else r+=l.isUpperCase(c)?l:l.toUpperCase(c);return r;}
  public static void main(String[] a){
    System.out.println(c("i CAN HARDLY WORK LIKE THIS please GET ME A NEW KEYBOARD FOR cHRISTMAS".toCharArray()));
    System.out.println(c("Hello World!".toCharArray()));
    System.out.println(c("I like pie :)".toCharArray()));
    System.out.println(c("hELP my KeYboarD\niS BROKEN".toCharArray()));
    System.out.println(c("cHRISTMAS IS COMING REALLY SOON!".toCharArray()));
    System.out.println(c("C is the first letter in cHRISTMAS".toCharArray()));
  }
}
```
**Output:**
```
I Can HaRdly work lIke thiS PLEASE geT Me A new keyboard for ChriStmas
hELLO wORLD!
i LIKE PIE :)
Help MY kEyBOARd
Is broken
cHRISTMAS is Coming really soon!
C IS ThE FIrST LETTER iN ChriSTMAS
```
[Answer]
# Python 100 bytes
```
def a(s,i=0,g=''):
 for c in s:a=c.lower()=='christmas'[i%9];i+=a;g+=[c.swapcase(),c][a]
 return g
```
[Answer]
# Ruby, 63+1 = 64 bytes
Uses the `-p` flag.
```
i=0;gsub(/./){|c|c=~/#{"christmas"[i%9]}/i?(i+=1;c):c.swapcase}
```
[Answer]
## C# 239 chars
```
var s=Console.ReadLine().ToCharArray();int j=0,i=0;var c="christmas";for(;j<s.Length;j++)if(s[j]==c[i%9]|s[j]==(c[i%9]-32))i++;else if(s[j]>64&s[j]<91)s[j]=(char)(s[j]+32);else if(s[j]>96&s[j]<123)s[j]=(char)(s[j]-32);Console.WriteLine(s);
```
more explicit version:
```
var s = Console.ReadLine().ToCharArray();
int j = 0,i = 0;
var c = "christmas";
for (var j = 0; j < s.Length; j++)
   if (s[j] == c[i%9]|s[j] == (c[i%9] - 32))// non case sensitive compare with c 
      i++;//next char in christmas
   else
      if (s[j] > 64 & s[j] < 91)//if between A and Z
         s[j] = (char)(s[j] + 32);//convert to a-z
      else
         if (s[j] > 96 & s[j] < 123)//if between a and z
            s[j] = (char)(s[j] - 32);//convert to A-Z
Console.WriteLine(s);
```
this is a pretty naïve solution, and probably can be improved (maybe we can allow implicit conversion to char?).
it assumes to be inside a function, reads from the console (stdin), and writes to it (stdout).
edit: Char.IsUpper(s[j]) is 2 bytes longer than s[j]>64&&s[j]<91, Char.ToUpper is longer than my version too.
[Answer]
# Haskell, ~~222~~ 207 Bytes
```
import Data.Char
s=(+(-65)).ord
f=(`divMod`32).s
l=[['a'..'z'],['A'..'Z']]
c=cycle$map s"CHRISTMAS"
k _[]=[]
k(a:b)(x:y)|isAlpha x=let(d,m)=f x in if(m==a)then(x:k b y)else(l!!d!!m:k(a:b)y)|1>0=x:k(a:b)y
main=interact$k c
```
---
updated:
```
import Data.Char
s=(+(-65)).ord
k _[]=[]
k(a:b)(x:y)|isAlpha x=let(d,m)=s x`divMod`32 in if(m==a)then(x:k b y)else([['a'..'z'],['A'..'Z']]!!d!!m:k(a:b)y)|1>0=x:k(a:b)y
main=interact$k$cycle$map s"CHRISTMAS"
```
## How it works:
> 
> 
> ```
> s=(+(-65)).ord
> 
> ```
> 
> s x = ASCII value of x - ASCII value of 'A'
> 
> 
> 
> ```
> f=(`divMod`32).s
> 
> ```
> 
> f (s x) = (0,s x) for uppercase, (1, (s x-32)) for lowercase
> 
> 
> 
> ```
> l=[['a'..'z'],['A'..'Z']]
> 
> ```
> 
> parallel list of letters, indexable by f (lowercase->1->uppercase, uppercase->0->lowercase)
> 
> 
> 
> ```
> c = cycle $ map s "CHRISTMAS"
> 
> ```
> 
> infinite list of the ascii values of uppercase christmas repeated
> 
> 
> 
> ```
> k _ []=[]
> 
> ```
> 
> base case
> 
> 
> 
> ```
> k (a:b) (x:y) | isAlpha x = let (d,m) =f x
>                              in if m == a
>                                 then x : k b y
>                                 else (l!!d)!!m : k (a:b) y
>               | 1 > 0 = x : k (a:b) y
> 
> ```
> 
> return non-alphanumeric characters, and either keep the letter if its s-value is the same as the current letter of christmas (going to the next letter), otherwise convert it to the other case and proceed
> 
> 
> 
> ```
> main=interact$k c
> 
> ```
> 
> IO
> 
> 
> 
]