Post History
C (gcc), 112 bytes Function solution. p,*a;f(char*s,int n){a=strdup(s);for(s=a;*s;s++)(p=isalpha(*s)?(*s&96)^96?65:97:0)&&(*s=(*s-p+n)%26+p);puts(a);} Try it online! Explanati...
Answer
#3: Post edited
- # [C (gcc)], 112 bytes
- Function solution.
- <!-- language-all: lang-c -->
- p,*a;f(char*s,int n){a=strdup(s);for(s=a;*s;s++)(p=isalpha(*s)?(*s&96)^96?65:97:0)&&(*s=(*s-p+n)%26+p);puts(a);}
- [Try it online!][TIO-ks906ega]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-ks906ega]: https://tio.run/##VY5vT8IwEMbf71McM5J1KyooIzAnL9XEz2BStq5rmG3tn7FB@OxYMoNyl7tLnjy/3FNMWFGcbrgoGldSeDa25PKufgn@S5oLdq0VtlfUSyeFY5JVUVETHRvMhQWBDiT3TOlUZFBWSR2ZnGSxyUySoEjl3JBG1SSKDVr7NV6m6HOZrtP5arlYPaDx2Iu5n4lKBLqdpYlCmXLWRARlx9P5xRfhIkLBIQCoovCNNo3EsJO6KUMMTyiDv7q/hw@ulMFATKvqgbBaNuf27un8yj4QnJXk3IObdbLbwr5mzMJ@IzvQtQDBQ7zwqHcL2lINTApBgPGWQi8dODXA77AjPvFGU7KtiLGjEM8uCT38Cq5vNOxV0fOy/9aj35@urYDpLQN/q7oK8fTxEtRjtuYGLO0s@GucCY7B6Qc "C (gcc) – Try It Online"
- ---
- Explanation: The code takes a hard copy of the passed parameter and increases each character with the value, but only in case it's a letter.
- isalpha determines if something is a letter, then `*s & 0x60` masks out lower case, since those always have bits 0x60 set (and upper always have 0x40 but not 0x60).
For upper case, p gets set to 'A', for lower case it gets set to 'a', otherwise to zero. The equation `*s=(*s-p+n)%26+p` only gets executed if p is not zero and basically just "modulo away" results larger than 26 - the size of the alphabet.
- # [C (gcc)], 112 bytes
- Function solution.
- <!-- language-all: lang-c -->
- p,*a;f(char*s,int n){a=strdup(s);for(s=a;*s;s++)(p=isalpha(*s)?(*s&96)^96?65:97:0)&&(*s=(*s-p+n)%26+p);puts(a);}
- [Try it online!][TIO-ks906ega]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-ks906ega]: https://tio.run/##VY5vT8IwEMbf71McM5J1KyooIzAnL9XEz2BStq5rmG3tn7FB@OxYMoNyl7tLnjy/3FNMWFGcbrgoGldSeDa25PKufgn@S5oLdq0VtlfUSyeFY5JVUVETHRvMhQWBDiT3TOlUZFBWSR2ZnGSxyUySoEjl3JBG1SSKDVr7NV6m6HOZrtP5arlYPaDx2Iu5n4lKBLqdpYlCmXLWRARlx9P5xRfhIkLBIQCoovCNNo3EsJO6KUMMTyiDv7q/hw@ulMFATKvqgbBaNuf27un8yj4QnJXk3IObdbLbwr5mzMJ@IzvQtQDBQ7zwqHcL2lINTApBgPGWQi8dODXA77AjPvFGU7KtiLGjEM8uCT38Cq5vNOxV0fOy/9aj35@urYDpLQN/q7oK8fTxEtRjtuYGLO0s@GucCY7B6Qc "C (gcc) – Try It Online"
- ---
- Explanation: The code takes a hard copy of the passed parameter and increases each character with the value, but only in case it's a letter.
- isalpha determines if something is a letter, then `*s & 0x60` masks out lower case, since those always have bits 0x60 set (and upper always have 0x40 but not 0x60).
- For upper case, p gets set to 'A', for lower case it gets set to 'a', otherwise to zero. The equation to handle wrap-around `*s=(*s-p+n)%26+p` only gets executed if p is not zero and basically just "modulo away" results larger than 26 - the size of the alphabet.
#2: Post edited
- # [C (gcc)], 112 bytes
- Function solution.
- <!-- language-all: lang-c -->
- p,*a;f(char*s,int n){a=strdup(s);for(s=a;*s;s++)(p=isalpha(*s)?(*s&96)^96?65:97:0)&&(*s=(*s-p+n)%26+p);puts(a);}
- [Try it online!][TIO-ks906ega]
- [C (gcc)]: https://gcc.gnu.org/
[TIO-ks906ega]: https://tio.run/##VY5vT8IwEMbf71McM5J1KyooIzAnL9XEz2BStq5rmG3tn7FB@OxYMoNyl7tLnjy/3FNMWFGcbrgoGldSeDa25PKufgn@S5oLdq0VtlfUSyeFY5JVUVETHRvMhQWBDiT3TOlUZFBWSR2ZnGSxyUySoEjl3JBG1SSKDVr7NV6m6HOZrtP5arlYPaDx2Iu5n4lKBLqdpYlCmXLWRARlx9P5xRfhIkLBIQCoovCNNo3EsJO6KUMMTyiDv7q/hw@ulMFATKvqgbBaNuf27un8yj4QnJXk3IObdbLbwr5mzMJ@IzvQtQDBQ7zwqHcL2lINTApBgPGWQi8dODXA77AjPvFGU7KtiLGjEM8uCT38Cq5vNOxV0fOy/9aj35@urYDpLQN/q7oK8fTxEtRjtuYGLO0s@GucCY7B6Qc "C (gcc) – Try It Online"
- # [C (gcc)], 112 bytes
- Function solution.
- <!-- language-all: lang-c -->
- p,*a;f(char*s,int n){a=strdup(s);for(s=a;*s;s++)(p=isalpha(*s)?(*s&96)^96?65:97:0)&&(*s=(*s-p+n)%26+p);puts(a);}
- [Try it online!][TIO-ks906ega]
- [C (gcc)]: https://gcc.gnu.org/
- [TIO-ks906ega]: https://tio.run/##VY5vT8IwEMbf71McM5J1KyooIzAnL9XEz2BStq5rmG3tn7FB@OxYMoNyl7tLnjy/3FNMWFGcbrgoGldSeDa25PKufgn@S5oLdq0VtlfUSyeFY5JVUVETHRvMhQWBDiT3TOlUZFBWSR2ZnGSxyUySoEjl3JBG1SSKDVr7NV6m6HOZrtP5arlYPaDx2Iu5n4lKBLqdpYlCmXLWRARlx9P5xRfhIkLBIQCoovCNNo3EsJO6KUMMTyiDv7q/hw@ulMFATKvqgbBaNuf27un8yj4QnJXk3IObdbLbwr5mzMJ@IzvQtQDBQ7zwqHcL2lINTApBgPGWQi8dODXA77AjPvFGU7KtiLGjEM8uCT38Cq5vNOxV0fOy/9aj35@urYDpLQN/q7oK8fTxEtRjtuYGLO0s@GucCY7B6Qc "C (gcc) – Try It Online"
- ---
- Explanation: The code takes a hard copy of the passed parameter and increases each character with the value, but only in case it's a letter.
- isalpha determines if something is a letter, then `*s & 0x60` masks out lower case, since those always have bits 0x60 set (and upper always have 0x40 but not 0x60).
- For upper case, p gets set to 'A', for lower case it gets set to 'a', otherwise to zero. The equation `*s=(*s-p+n)%26+p` only gets executed if p is not zero and basically just "modulo away" results larger than 26 - the size of the alphabet.
#1: Initial revision
# [C (gcc)], 112 bytes Function solution. <!-- language-all: lang-c --> p,*a;f(char*s,int n){a=strdup(s);for(s=a;*s;s++)(p=isalpha(*s)?(*s&96)^96?65:97:0)&&(*s=(*s-p+n)%26+p);puts(a);} [Try it online!][TIO-ks906ega] [C (gcc)]: https://gcc.gnu.org/ [TIO-ks906ega]: https://tio.run/##VY5vT8IwEMbf71McM5J1KyooIzAnL9XEz2BStq5rmG3tn7FB@OxYMoNyl7tLnjy/3FNMWFGcbrgoGldSeDa25PKufgn@S5oLdq0VtlfUSyeFY5JVUVETHRvMhQWBDiT3TOlUZFBWSR2ZnGSxyUySoEjl3JBG1SSKDVr7NV6m6HOZrtP5arlYPaDx2Iu5n4lKBLqdpYlCmXLWRARlx9P5xRfhIkLBIQCoovCNNo3EsJO6KUMMTyiDv7q/hw@ulMFATKvqgbBaNuf27un8yj4QnJXk3IObdbLbwr5mzMJ@IzvQtQDBQ7zwqHcL2lINTApBgPGWQi8dODXA77AjPvFGU7KtiLGjEM8uCT38Cq5vNOxV0fOy/9aj35@urYDpLQN/q7oK8fTxEtRjtuYGLO0s@GucCY7B6Qc "C (gcc) – Try It Online"