53
\$\begingroup\$

Challenge

Given a non-empty string S of length L consisting entirely of printable ASCII chars, output another string of length L that consists entirely of printable ASCII chars, but is not equal to S.

For the purposes of this challenge, a printable ASCII char is one between U+0020 and U+007E, inclusive; that is, from (space) to ~ (tilde). Newlines and tabs are not included.

For example, given "abcde", some valid outputs could be:

  • "11111"
  • "abcdf"
  • "edcba"

But these would be invalid:

  • "abcde"
  • "bcde"
  • "abcde0"

Test cases

"asdf"
"1111"
"       "
"~~~~~"
"abcba"
"1"
" "
"~"
" ~"
"~ "
"  0"
"!@#$%^&*()ABCDEFGhijklmnop1234567890"
" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

Rules

  • You may assume the input consists entirely of printable ASCII chars.
  • You may not assume that the input does not contain all 95 printable chars.
  • You may assume the input contains at least one character and is less than 256 chars long.
  • The output must also consist entirely of printable ASCII chars. You could not, for example, output the byte \x7F for input "~".
  • The output must be different than the input with probability 1; that is, you may generate random strings until one is different than the input, but you can't just output L random characters and hope it's different.
  • Newlines are disallowed in the output, but you may output one trailing newline which is not counted toward the string.

Scoring

This is , so the shortest code in bytes in each language wins.

\$\endgroup\$
  • \$\begingroup\$ Note that "positive" excludes the empty string. For extra clarity, maybe replace "positive" with "nonzero"? \$\endgroup\$ – CalculatorFeline May 30 '17 at 20:30
  • 5
    \$\begingroup\$ @CalculatorFeline But that would include negative-length strings /s \$\endgroup\$ – ETHproductions May 30 '17 at 20:36
  • 1
    \$\begingroup\$ ...Those don't exist. \$\endgroup\$ – CalculatorFeline May 30 '17 at 20:38
  • \$\begingroup\$ @CalculatorFeline Better now? \$\endgroup\$ – ETHproductions May 30 '17 at 20:40
  • 3
    \$\begingroup\$ Another simple but not trivial challenge. \$\endgroup\$ – Weijun Zhou Apr 4 '18 at 15:03

69 Answers 69

0
\$\begingroup\$

BrainFuck, 49 47 Bytes

,>++++[-<-------->]++[<]++++[->++++++++<]>-[.,]
x           0   (x-32) 2                (x-32||2)+31
^           ^(x>32) ^(x==32)

Assuming EOF as zero

\$\endgroup\$
  • \$\begingroup\$ You can cut out the first set of ., and just go straight into the loop \$\endgroup\$ – Jo King Dec 26 '17 at 15:54
  • \$\begingroup\$ Thanks \$\endgroup\$ – l4m2 Dec 27 '17 at 0:26
0
\$\begingroup\$

Perl 5, 23 + 1 (-p) = 24 bytes

s/./chr 65+ord($&)%26/e

Try it online!

Blatantly steals @FlipTack's method to change the first character to a different upper case letter.

\$\endgroup\$
  • \$\begingroup\$ You can drop one of the digits in 26, at least. \$\endgroup\$ – Ørjan Johansen Jan 11 '18 at 0:51
0
\$\begingroup\$

Julia 0.6, 26 bytes

s->map(x->x<'b'?'b':'a',s)

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Fortran (GFortran), 77 bytes

CHARACTER(256)A
READ*,A
A(1:1)=CHAR(MOD(IACHAR(A(1:1))+1,94)+33)
PRINT*,A
END

Try it online!

It does what was required... well, with some issues...

1) If the first character is "or <space>, they will be ignored;

2) If there is /, it will be understood as something like "end of transmission";

3) There will be a leading space (which is not part of the output (so do I think), because Fortran always prints a leading space if the output is not formated) and up to 255 trailing spaces. This last problem (i.e., the trailing spaces) can be solved using no more then six bytes.

\$\endgroup\$
0
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 71 bytes

	INPUT LEN(1) . X REM . S
	X =IDENT(X) 1	:S(O)
	X =0
O	OUTPUT =X S
END	

Try it online!

Replaces the first character of S with a 0 unless it's already, 0 in which case it swaps it with 1.

\$\endgroup\$
0
\$\begingroup\$

Pyth, 12 bytes

+C+%ChQlG32t

Test suite

Python 3 translation:
Q=eval(input())
print(chr(ord(Q[0])%26+32)+Q[1:])
\$\endgroup\$
0
\$\begingroup\$

Swift 3, 76 68 bytes

let s=readLine()!;print(s.characters.map{$0=="a" ?"b":"a"}.joined())

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Gol><>, 6 bytes

iE;5%n

Try it online!

Direct translation of Jo King's ><> answer.

\$\endgroup\$
0
\$\begingroup\$

Tcl, 56 bytes

proc C s {join [lmap x [split $s ""] {expr {$x==0}}] ""}

Try it online!

Approach very similar to last one: replaces every non-zero by a zero and every zero by one.


Tcl, 62 bytes

proc C s {puts [expr {[string in $s 0]==0}][string ra $s 1 e]}

Try it online!

Approach replaces first character by 1 if it is a 0, else it replaces anything else by a 0.

Tcl, 83 bytes

proc C s {scan [string in $s 0] %c f
puts "[format %c [incr f]][string ra $s 1 e]"}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.