Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Post History

60%
+1 −0
Challenges Caesar shift cipher

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...

posted 3y ago by Lundin‭  ·  edited 3y ago by Lundin‭

Answer
#3: Post edited by user avatar Lundin‭ · 2021-08-12T14:33:50Z (over 3 years ago)
  • # [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 by user avatar Lundin‭ · 2021-08-12T14:21:53Z (over 3 years ago)
  • # [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 by user avatar Lundin‭ · 2021-08-12T14:15:58Z (over 3 years ago)
# [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"