Skip to main content
added 154 characters in body; added 81 characters in body
Source Link
O.O.Balance
  • 1.6k
  • 1
  • 9
  • 19

C++ 14 (g++), 181 171 148 147147 134 bytes

[](auto s){s[0]&=95;int i=1,n=s.length()-1,b;for(;s[i];)s[i++]|=32;b=s[n]s[i++]|=32;b=s[--i]-'y';return s+(b?"y":"")+" Mc"+(b?s:s.substr(0,s[ns[i-1]-'e'?ni:ni-1))+"face";}

Note that clang will not compile this.

Credit goes to Kevin Cruijssen and Olivier GrégoireOlivier Grégoire for the &95 trick.

Thanks to Chris for golfing 11 bytes.

Try it online herehere.

Ungolfed version:

[] (auto s) { // lambda taking an std::string as argument and returning an std::string
    s[0] &= 95; // convert the first character to upper case
    int n = s.length()-1, // the position of the last character
    i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    // i is now s.length()
    b = s[n]s[--i] - 'y'; // whether the last character is not a 'y'
    // i is now s.length()-1
    return s + (b ? "y" : "") // append 'y' if not already present
    + " Mc"
    + (b ? s : s.substr(0, s[ns[i-1] - 'e' ? n-1i : ni-1)) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}

C++ 14 (g++), 181 171 148 147 bytes

[](auto s){s[0]&=95;int i=1,n=s.length()-1,b;for(;s[i];)s[i++]|=32;b=s[n]-'y';return s+(b?"y":"")+" Mc"+(b?s:s.substr(0,s[n-1]-'e'?n:n-1))+"face";}

Note that clang will not compile this.

Credit goes to Kevin Cruijssen and Olivier Grégoire for the &95 trick.

Try it online here.

Ungolfed version:

[] (auto s) { // lambda
    s[0] &= 95; // convert the first character to upper case
    int n = s.length()-1, // the position of the last character
    i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    b = s[n] - 'y'; // whether the last character is not a 'y'
    return s + (b ? "y" : "") // append 'y' if not already present
    + " Mc"
    + (b ? s : s.substr(0, s[n-1] - 'e' ? n-1 : n)) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}

C++ 14 (g++), 181 171 148 147 134 bytes

[](auto s){s[0]&=95;int i=1,b;for(;s[i];)s[i++]|=32;b=s[--i]-'y';return s+(b?"y":"")+" Mc"+(b?s:s.substr(0,s[i-1]-'e'?i:i-1))+"face";}

Note that clang will not compile this.

Credit goes to Kevin Cruijssen and Olivier Grégoire for the &95 trick.

Thanks to Chris for golfing 11 bytes.

Try it online here.

Ungolfed version:

[] (auto s) { // lambda taking an std::string as argument and returning an std::string
    s[0] &= 95; // convert the first character to upper case
    int i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    // i is now s.length()
    b = s[--i] - 'y'; // whether the last character is not a 'y'
    // i is now s.length()-1
    return s + (b ? "y" : "") // append 'y' if not already present
    + " Mc"
    + (b ? s : s.substr(0, s[i-1] - 'e' ? i : i-1)) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}
deleted 11 characters in body
Source Link
O.O.Balance
  • 1.6k
  • 1
  • 9
  • 19

C++ 14 (g++), 181 171 148148 147 bytes

[](auto s){s[0]&=~32;ints[0]&=95;int i=1,n=s.length()-1,b;for(;s[i];)s[i++]|=32;b=s[n]-'y';return s+(b?"y":"")+" Mc"+(b?s:s.substr(0,s[n-1]-'e'?n:n-1))+"face";}   

Note that clang will not compile this.

Credit goes to Kevin Cruijssen and Olivier Grégoire for the &~32&95 trick and for helping me shave off 10 bytes.

Try it online herehere.

Ungolfed version:

[] (auto s) { // lambda
    s[0] &= ~32;95; // convert the first character to upper case
    int n = s.length()-1, // the position of the last character
    i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    b = s[n] - 'y'; // whether the last character is not a 'y'
    return s + (b ? "y" : "") // append 'y' if not already present
    + " Mc"
    + (b ? s : s.substr(0, s[n-1] - 'e' ? n-1 : n)) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}

C++ 14 (g++), 181 171 148 bytes

[](auto s){s[0]&=~32;int i=1,n=s.length()-1,b;for(;s[i];)s[i++]|=32;b=s[n]-'y';return s+(b?"y":"")+" Mc"+(b?s:s.substr(0,s[n-1]-'e'?n:n-1))+"face";}   

Note that clang will not compile this.

Credit goes to Kevin Cruijssen for the &~32 trick and for helping me shave off 10 bytes.

Try it online here.

Ungolfed version:

[] (auto s) { // lambda
    s[0] &= ~32; // convert the first character to upper case
    int n = s.length()-1, // the position of the last character
    i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    b = s[n] - 'y'; // whether the last character is not a 'y'
    return s + (b ? "y" : "") // append 'y' if not already present
    + " Mc"
    + (b ? s : s.substr(0, s[n-1] - 'e' ? n-1 : n)) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}

C++ 14 (g++), 181 171 148 147 bytes

[](auto s){s[0]&=95;int i=1,n=s.length()-1,b;for(;s[i];)s[i++]|=32;b=s[n]-'y';return s+(b?"y":"")+" Mc"+(b?s:s.substr(0,s[n-1]-'e'?n:n-1))+"face";}

Note that clang will not compile this.

Credit goes to Kevin Cruijssen and Olivier Grégoire for the &95 trick.

Try it online here.

Ungolfed version:

[] (auto s) { // lambda
    s[0] &= 95; // convert the first character to upper case
    int n = s.length()-1, // the position of the last character
    i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    b = s[n] - 'y'; // whether the last character is not a 'y'
    return s + (b ? "y" : "") // append 'y' if not already present
    + " Mc"
    + (b ? s : s.substr(0, s[n-1] - 'e' ? n-1 : n)) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}
deleted 151 characters in body; added 3 characters in body
Source Link
O.O.Balance
  • 1.6k
  • 1
  • 9
  • 19

C++ 14 (gccg++), 181 171171 148 bytes

#include<string>
auto m[](auto s){s[0]&=~32;int i=1,n=s.length()-1,b;for(;s[i];)s[i++]|=32;b=s[n]=='y';returns[i++]|=32;b=s[n]-'y';return s+(b?"":"y":"")+" Mc"+(b?s:s.substr(0,s[n-1]=='e'1]-'e'?n-1:n-1):s)+"face";}    

Note that clang will not compile this due to the use of auto in a function prototype/return type.

Credit goes to Kevin Cruijssen for the &~32 trick and for helping me shave off 10 bytes.

Try it online herehere.

Ungolfed version:

#include <string>
auto[] m(auto s) { // abuse of auto, input and output are std::stringlambda
    s[0] &= ~32; // convert the first character to upper case
    int n = s.length()-1, // the position of the last character
    i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    b = s[n] ==- 'y'; // whether or not the last character is not a 'y'
    return s + (b ? """y" : "y""") // append 'y' if not already present
    + " Mc"
    + (b ? s : s.substr(0, s[n-1] ==- 'e' ? n-1 : n) : s) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}

C++ (gcc), 181 171 bytes

#include<string>
auto m(auto s){s[0]&=~32;int i=1,n=s.length()-1,b;for(;s[i];)s[i++]|=32;b=s[n]=='y';return s+(b?"":"y")+" Mc"+(b?s.substr(0,s[n-1]=='e'?n-1:n):s)+"face";}    

Note that clang will not compile this due to the use of auto in a function prototype/return type.

Credit goes to Kevin Cruijssen for the &~32 trick and for helping me shave off 10 bytes.

Try it online here.

Ungolfed version:

#include <string>
auto m(auto s) { // abuse of auto, input and output are std::string
    s[0] &= ~32; // convert the first character to upper case
    int n = s.length()-1, // the position of the last character
    i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    b = s[n] == 'y'; // whether or not the last character is a 'y'
    return s + (b ? "" : "y") // append 'y' if not already present
    + " Mc"
    + (b ? s.substr(0, s[n-1] == 'e' ? n-1 : n) : s) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}

C++ 14 (g++), 181 171 148 bytes

[](auto s){s[0]&=~32;int i=1,n=s.length()-1,b;for(;s[i];)s[i++]|=32;b=s[n]-'y';return s+(b?"y":"")+" Mc"+(b?s:s.substr(0,s[n-1]-'e'?n:n-1))+"face";}   

Note that clang will not compile this.

Credit goes to Kevin Cruijssen for the &~32 trick and for helping me shave off 10 bytes.

Try it online here.

Ungolfed version:

[] (auto s) { // lambda
    s[0] &= ~32; // convert the first character to upper case
    int n = s.length()-1, // the position of the last character
    i = 1, // for iterating over the string
    b; // we'll need this later
    for(; s[i] ;) // iterate over the rest of the string
        s[i++] |= 32; // converting it to lower case
    b = s[n] - 'y'; // whether the last character is not a 'y'
    return s + (b ? "y" : "") // append 'y' if not already present
    + " Mc"
    + (b ? s : s.substr(0, s[n-1] - 'e' ? n-1 : n)) // remove one, two, or zero chars from the end depending on b and whether the second to last character is 'e'
    + "face";
}
golfed by suggestion of Kevin Cruijssen
Source Link
O.O.Balance
  • 1.6k
  • 1
  • 9
  • 19
Loading
added another test case since question was edited
Source Link
O.O.Balance
  • 1.6k
  • 1
  • 9
  • 19
Loading
Source Link
O.O.Balance
  • 1.6k
  • 1
  • 9
  • 19
Loading