We haven't had a string question for a while (5 days to be precise), so let's go for one.
Given a string s and a positive integer n, take every nth element of s, repeat it n times, and put it back into s.
For example, if n = 3 and s = "Hello, World!", every third character is Hl r!. You then repeat each character n times to produce HHHlll rrr!!!. You then replace the original letters with the repeated versions to produce the final product of HHHellllo, Worrrld!!!
You are to accomplish this task in the shortest code possible in your language!
Rules
- This is a code-golf so the shortest code in bytes wins
nis guaranteed to be smaller than the length ofsand greater than 0- The first character of
sis where thenth characters are taken from, and is always repeatedntimes swill only consist of printable ASCII (code points0x20 (space)to0x7E (~))
Test cases
s, n => output
"Hello, World!", 3 => "HHHellllo, Worrrld!!!"
"Code golf", 1 => "Code golf"
"abcdefghijklm", 10 => "aaaaaaaaaabcdefghijkkkkkkkkkklm"
"tesTing", 6 => "ttttttesTingggggg"
"very very very long string for you to really make sure that your program works", 4 => "vvvvery veryyyy verrrry loooong sssstrinnnng foooor yoooou toooo reaaaally makeeee surrrre thhhhat yyyyour proggggram workkkks"
29 Answers 29
Jelly, 3 bytes
Ḣs×ばつ
Input is taken as s, n.
How it works
Ḣs×ばつ Main link. Argument: s, n
Ḣ Head; yield s.
This pops the list, leaving [n] as the main link's argument.
s Split s into chunks of length n.
×ばつ Multiply each chunk by [n], repeating its first element n times.
-
\$\begingroup\$ Isn't it actually 6 bytes in UTF-8 encoding?
E1 B8 A2 73 C3 97\$\endgroup\$CodeManX– CodeManX2017年10月12日 12:58:46 +00:00Commented Oct 12, 2017 at 12:58 -
5\$\begingroup\$ In UTF-8, yes. However, Jelly uses a custom code page, where each of the characters it understands takes up only a single byte. \$\endgroup\$Dennis– Dennis2017年10月12日 14:05:32 +00:00Commented Oct 12, 2017 at 14:05
Jelly, (削除) 6 (削除ここまで) 5 bytes
-1 byte thanks to leaky Nun (use Python's string multiplication.)
×ばつJm\¦
A full program, accepting two command line arguments, the string and the number, and printing the result.
How?
×ばつJm\¦ - Main link: list of characters, s; number, n e.g. ['g','o','l','f','e','r'], 2
¦ - sparse application:
\ - ...to indices: last two links as a dyad:
J - range of length of s [1,2,3,4,5,6]
m - modulo slicing by n (every nth entry) ×ばつ - ...action: multiply ["gg",'o',"ll",'f',"ee",'r']
- implicit print >>> ggollfeer
-
-
\$\begingroup\$ Yep tried
xforgot×; thanks. \$\endgroup\$Jonathan Allan– Jonathan Allan2017年10月12日 07:20:21 +00:00Commented Oct 12, 2017 at 7:20 -
\$\begingroup\$ Isn't it actually 8 bytes in UTF-8 encoding?
C3 97 4A 6D C2 A5 C2 A6\$\endgroup\$CodeManX– CodeManX2017年10月12日 13:00:37 +00:00Commented Oct 12, 2017 at 13:00 -
2
-
\$\begingroup\$ @MDXF thanks for fielding! \$\endgroup\$Jonathan Allan– Jonathan Allan2017年10月12日 16:22:21 +00:00Commented Oct 12, 2017 at 16:22
JavaScript (ES6), 46 bytes
Takes input in currying syntax (s)(n).
s=>n=>s.replace(/./g,(c,i)=>c.repeat(i%n?1:n))
Test cases
let f =
s=>n=>s.replace(/./g,(c,i)=>c.repeat(i%n?1:n))
console.log(f("Hello, World!")(3))
console.log(f("Code golf")(1))
console.log(f("abcdefghijklm")(10))
console.log(f("tesTing")(6))
console.log(f("very very very long string for you to really make sure that your program works")(4))
C# (.NET Core), (削除) 84 (削除ここまで) 82 bytes
n=>m=>{var s="";for(int i=0;i<n.Length;)s+=new string(n[i],i++%m<1?m:1);return s;}
-
\$\begingroup\$ You can save a byte by removing
i++and changingn[i],i%m<1?m:1ton[i],i++%m<1?m:1. \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年10月12日 07:46:22 +00:00Commented Oct 12, 2017 at 7:46 -
\$\begingroup\$ You can save another byte by currying the inputs:
n=>m=>...\$\endgroup\$raznagul– raznagul2017年10月12日 09:37:04 +00:00Commented Oct 12, 2017 at 9:37
05AB1E, (削除) 8 (削除ここまで) 7 bytes
-1 byte thanks to @Emigna
×ばつì?
Explanation
×ばつì? Arguments s, n ("Hello, World!", 3)
ô Split s into pieces of n (["Hel", "lo,", ...])
ʒ Filter (used as foreach)
ć Extract head ("Hel" -> "el", "H" ...)
×ばつì Repeat n times and prepend ("el", "H" -> "HHHel" ...)
? Print without newline
-
\$\begingroup\$ Save a byte with
ôʒć²×ì?\$\endgroup\$Emigna– Emigna2017年10月12日 11:04:42 +00:00Commented Oct 12, 2017 at 11:04 -
\$\begingroup\$ @Emigna thanks, I knew there must be a way to get rid of the closing
}\$\endgroup\$kalsowerus– kalsowerus2017年10月12日 11:15:32 +00:00Commented Oct 12, 2017 at 11:15 -
\$\begingroup\$ Weird use of filter when it isn't using the result but it actually makes a difference... \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年10月12日 20:05:27 +00:00Commented Oct 12, 2017 at 20:05
-
\$\begingroup\$ @MagicOctopusUrn, yep filter is still the better foreach in 05AB1E \$\endgroup\$kalsowerus– kalsowerus2017年10月12日 20:19:21 +00:00Commented Oct 12, 2017 at 20:19
-
\$\begingroup\$ @kalsowerus
vyis one foreach,εis another. Oddly enough,εdoesn't work. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年10月12日 20:22:16 +00:00Commented Oct 12, 2017 at 20:22
Python 2, (削除) 54 (削除ここまで) 53 bytes
Edit: Saved 1 byte thanks to @Rod
f=lambda x,n,i=0:x[i:]and[1,n][i%n<1]*x[i]+f(x,n,i+1)
-
1\$\begingroup\$ you can swap
x[i]and[1,n][i%n<1]to save a space \$\endgroup\$Rod– Rod2017年10月11日 19:48:45 +00:00Commented Oct 11, 2017 at 19:48
Alice, 25 bytes
/
KI /!Iw?&.?t&O?&wWOI.h%
Explanation
/ Switch to Ordinal.
I Read first line of input (i.e. n).
/ Switch to Cardinal.
! Convert input to its integer value and store it on the tape.
I Read first character from input string.
w Push current IP address onto the return address stack. This
effectively marks the beginning of the main loop.
? Retrieve n.
&. Duplicate current character n times (once more than we need,
but who cares about a clean stack...).
?t Retrieve n and decrement.
&O Output n-1 copies of the current character.
? Retrieve n.
&w Push the current IP address onto the return address stack n
times. This marks the beginning of a loop that is executed n
times.
W Discard one copy of the return address from the stack,
effectively decrementing the loop counter.
O Output the last character. On the first iteration, this is
the final copy of the repeated character, otherwise it's just
the single character we read on the last iteration.
I Read a character for the next iteration.
.h% Compute c % (c+1) on that character, which is a no-op for
for valid characters, but terminates the program at EOF when
c becomes -1.
K Jump to the address on top of the return address stack. As long
as there are still copies of the address from the inner loop, we
perform another iteration of that, otherwise we jump back to the
beginning of the outer loop.
R, (削除) 82 (削除ここまで) (削除) 76 (削除ここまで) 75 bytes
function(s,n)cat(rep(S<-el(strsplit(s,'')),c(n,rep(1,n-1))+!seq(S)),sep='')
A function; takes a string s and an integer n, and prints the repeated version to stdout.
Explanation:
function(s,n){
S <- el(strsplit(s,"")) # characters
r <- c(n,rep(1,n-1)) # [n, 1, 1,...,1], length n
repeats <- r+!seq(S) # extends R to length of S
cat(rep(S, repeats), sep="") # print out
}
R, 55 bytes
function(S,n)cat(rep(S,c(n,rep(1,n-1))+!seq(S)),sep="")
Same algorithm as above, but with S taken as a list of individual characters.
PowerShell, 51 bytes
param($a,$n)-join($a|%{($_,("$_"*$n))[!($i++%$n)]})
Takes input as a char-array $a and the number $n. Loops through $a and each iteration either outputs the current letter $_ or the current letter multiplied by $n, based on an index into a pseudo-ternary. The index chooses between the two based off of incrementing $i and then modulo $n. Those letters are then -joined back together and the string is left on the pipeline; output is implicit.
Japt, 8 bytes
ËùDV*EvV
Explanation
Ë ùDV*EvV
UmDE{DùDV*EvV} Ungolfed
Implicit: U = s, V = n
UmDE{ } Replace each char D and (0-)index E in U by this function:
EvV Take 1 if the index is divisible by V; 0 otherwise.
V* Multiply this by V. This gives V for every Vth index; 0 for others.
DùD Pad D with itself to this length. This gives V copies of D for every
Vth index; 1 copy of D for others.
Implicit: output last expression
I have to credit the idea to use ù to @Shaggy's answer here. I don't know that I ever would have thought of it myself...
-
\$\begingroup\$ You see now why was so keen to see string padding added :) Nice solution. I was trying to get something to work with
ë, for poops & giggles, but failed miserably! \$\endgroup\$Shaggy– Shaggy2017年10月11日 21:26:49 +00:00Commented Oct 11, 2017 at 21:26
J, 17 bytes
(#@]$[,1#~<:@[)#]
(...) # ]everything in parens creates the string for J's built in "copy" verb. So, eg, if the left argument is 3, it creates the string3 1 1repeated as needed to equal the number of characters in the right arg], which contains the string. Which is to say,#solves the problem directly, assuming we can give it the correct left argument:4should be4 1 1 1repeated, and so on.- Examining
#@]$[,1#~<:@[, we see it uses J's shape verb$in the middle -- that's the main verb of this phrase... - To the left of
$is#@], meaning the length#of the right arg]. - To the right of
$is[,1#~<:@[, a 5 verb train. The first train executed is... 1#~<:@[, which means 1 copied#~(passive form of copy) one less than<:the left arg[. This result is passed to the final fork:[, ...meaning take the left arg, and append the result we just calculated, which is a string of1s.
-
\$\begingroup\$
]#~[^0=(|i.@#)for 14 bytes \$\endgroup\$miles– miles2017年10月13日 11:55:05 +00:00Commented Oct 13, 2017 at 11:55 -
\$\begingroup\$ That's quite clever. Your improvements to my posts are the best part of this site for me. \$\endgroup\$Jonah– Jonah2017年10月13日 14:16:27 +00:00Commented Oct 13, 2017 at 14:16
C# (.NET Core), 61 + 18 = 79 bytes
using System.Linq;
n=>m=>string.Concat(n.Select((c,i)=>new string(c,i%m<1?m:1)))
Perl 5, (削除) 37 (削除ここまで), 29 +1 (-p) bytes
-8 bytes thanks to Tom's comment.
$n=<>;s/./"@-"%$n?$&:$&x$n/ge
-
\$\begingroup\$ Can't think of a better approach right now, but I came up with a few ideas:
$n=<>;instead of theBEGINblock and havenon the next line of input and replace$-[0]with"@-"since only the first number is evaluated in comparison. Also, if you take input ofnvia-iyou can just use$^Iinstead of declaring and using$n, but since this is non-standard it might not fly... :) \$\endgroup\$Dom Hastings– Dom Hastings2017年10月12日 11:33:18 +00:00Commented Oct 12, 2017 at 11:33
6502 machine code routine, 50 bytes
A0 01 84 97 88 84 9E 84 9F B1 FB F0 20 A4 9F 91 FD C6 97 D0 10 A6 FF CA F0
05 C8 91 FD D0 F8 84 9F A5 FF 85 97 E6 9E A4 9E E6 9F D0 DC A4 9F 91 FD 60
This is a position-independent subroutine expecting a pointer to the input string (0-terminated aka C-string) in $fb/$fc, a pointer to the output buffer in $fd/$fe and the count (n) in $ff. It uses simple indexing, so it's limited to a maximum output length of 255 characters (+ 0 byte) due to the 8bit architecture.
Explanation (commented disassembly):
.rep:
A0 01 LDY #01ドル ; init counter to next repetition sequence
84 97 STY 97ドル
88 DEY ; init read and write index
84 9E STY 9ドルE ; (read)
84 9F STY 9ドルF ; (write)
.rep_loop:
B1 FB LDA ($FB),Y ; read next character
F0 20 BEQ .rep_done ; 0 -> finished
A4 9F LDY 9ドルF ; load write index
91 FD STA ($FD),Y ; write next character
C6 97 DEC 97ドル ; decrement counter to nex rep. seq.
D0 10 BNE .rep_next ; not reached yet -> next iteration
A6 FF LDX $FF ; load repetition counter
.rep_seqloop:
CA DEX ; and decrement
F0 05 BEQ .rep_seqdone ; if 0, no more repetitions
C8 INY ; increment write index
91 FD STA ($FD),Y ; write character
D0 F8 BNE .rep_seqloop ; and repeat for this sequence
.rep_seqdone:
84 9F STY 9ドルF ; store back write index
A5 FF LDA $FF ; re-init counter to next ...
85 97 STA 97ドル ; ... repetition sequence
.rep_next:
E6 9E INC 9ドルE ; increment read index
A4 9E LDY 9ドルE ; load read index
E6 9F INC 9ドルF ; increment write index
D0 DC BNE .rep_loop ; jump back (main loop)
.rep_done:
A4 9F LDY 9ドルF ; load write index
91 FD STA ($FD),Y ; and write terminating0-byte there
60 RTS ; done.
Example C64 machine code program using it:
This is a program in ca65-style assembler for the C64 using this routine (imported as rep):
REP_IN = $fb
REP_IN_L = $fb
REP_IN_H = $fc
REP_OUT = $fd
REP_OUT_L = $fd
REP_OUT_H = $fe
REP_N = $ff
.import rep
.segment "LDADDR"
.word $c000
.code
jsr $aefd ; consume comma
jsr $ad9e ; evaluate expression
sta REP_IN_L ; store string length
jsr $b6a3 ; free string
ldy #00ドル ; loop over string
readloop: cpy REP_IN_L ; end of string?
beq termstr ; then jump to 0-terminate string
lda (22ドル),y ; read next character
sta in,y ; store in input buffer
iny ; next
bne readloop
termstr: lda #00ドル ; load 0 byte
sta in,y ; store in input buffer
jsr $b79b ; read 8bit unsigned int
stx REP_N ; store in `n`
lda #<in ; (
sta REP_IN_L ; store pointer to
lda #>in ; to input string
sta REP_IN_H ; )
lda #<out ; (
sta REP_OUT_L ; store pointer to
lda #>out ; output buffer
sta REP_OUT_H ; )
jsr rep ; call function
ldy #00ドル ; output result
outloop: lda out,y
beq done
jsr $ffd2
iny
bne outloop
done: rts
.bss
in: .res 100ドル
out: .res 100ドル
Usage: sys49152,"[s]",[n], e.g. sys49152,"Hello, World!",3
Important: If the program was load from disk (like in the online demo), issue a new command first! This is necessary because loading a machine program trashes some C64 BASIC pointers.
Java 8, (削除) 100 (削除ここまで) 76 bytes
s->n->{int i,k=0;for(char c:s)for(i=k++%n<1?n:1;i-->0;)System.out.print(c);}
-24 bytes thanks to @OliverGrégoire.
Explanation:
s->n->{ // Method with char-array and int parameters and no return-type
int i,k=0; // Index-integers
for(char c:s) // Loop (1) over the characters of the input array
for(i=k++%n<1? // If `k` is divisible by the input `n`:
n // Change `i` to `n`
: // Else:
1; // Change `i` to 1
i-->0;) // Inner loop (2) from `i` down to 0
System.out.print(c); // And print the current character that many times
// End of inner loop (2) (implicit / single-line body)
// End of loop (1) (implicit / single-line body)
} // End of method
-
\$\begingroup\$ Oops, I didn't see there was already a submission so I deleted mine. Here's it, shortened to 76 bytes:
n->s->{int i,k=0;for(char c:s)for(i=k++%n<1?n:1;i-->0;)System.out.print(c);}(with achar[], instead ofString.) \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年10月12日 10:29:14 +00:00Commented Oct 12, 2017 at 10:29 -
\$\begingroup\$ Rule of the thumb, if you have to declare exactly one String that will be returned, it's shorter to just print it out. \$\endgroup\$Olivier Grégoire– Olivier Grégoire2017年10月12日 10:41:21 +00:00Commented Oct 12, 2017 at 10:41
-
\$\begingroup\$ @OlivierGrégoire Oops.. Yes, I know that rule of thumb, just forgot to apply it this time.. And thanks for the saved bytes! \$\endgroup\$Kevin Cruijssen– Kevin Cruijssen2017年10月13日 07:17:54 +00:00Commented Oct 13, 2017 at 7:17
MATL, (削除) 10 (削除ここまで) 7 bytes
-3 bytes thanks to Luis Mendo!
tq:ghY"
Takes input as n and then S as a string/char array.
% (implicit input)
% stack: n
t % duplicate
% stack: n n
q % decrement
% stack: n n-1
: % range
% stack: n [1 2 ... n-1]
g % convert to logical (nonzero->1, zero->0)
% stack: n [1 1 ... 1]
h % horizontal concatenate
% stack: [n 1 1 ... 1]
Y" % run-length decoding, taking the string as first input and recycling
% the lengths [n 1 1 ... 1] as needed
% (implicit output as string)
Haskell, (削除) 51 (削除ここまで) 46 bytes
Thanks @Laikoni for saving me 5 bytes!
n&s=do(m,c)<-zip[0..]s;c<$[0..(n-1)*0^mod m n]
Explanation/Ungolfed
The operator c <$ [a..b] replaces each element of the list [a,a+1...b] by c - so it's just a golfed replicate:
do(m,c)<-zip[0..]s; -- with all (m,c) in the enumerated ([(0,a),(1,b)..]) input string, replace with
replicate (1 + (n-1)*0^mod m n) c -- either n or 1 times the character c (note that the list begins with 0, that's where the 1+ comes from)
-
\$\begingroup\$ Would indexing into the string rather than using
enumeratebe shorter? \$\endgroup\$2017年10月11日 19:46:45 +00:00Commented Oct 11, 2017 at 19:46 -
\$\begingroup\$ @cairdcoinheringaahing i would have to use
range(len())in the end would be longer \$\endgroup\$Rod– Rod2017年10月11日 19:49:30 +00:00Commented Oct 11, 2017 at 19:49 -
Charcoal, 14 bytes
×ばつιθω
Try it online! Link is to verbose version of code. Takes input in the order n, s.
-
\$\begingroup\$ For some reason the TIO link has the arguments reversed. It was also for an older version of Charcoal; the
-slargument has since been renamed to-l. Current Charcoal also has theStringMapfunction which would save two bytes as it makes theJoinunnecessary. \$\endgroup\$Neil– Neil2020年10月12日 10:33:45 +00:00Commented Oct 12, 2020 at 10:33
V, 13 bytes
"aDJòylÀpÀll
This is a really dumb workaround. òlhÀälÀlÀ<M-->l should work, but I can't for the life of me understand why, especially since manually doing lhÀälÀlÀ<M-->l repeated a bunch of times does work.
Hexdump:
00000000: 1822 6144 4af2 796c c070 c06c 6c ."aDJ.yl.p.ll
Explanation:
<C-x> " Decrement the number
D " Delete that number...
"a " Into register 'a'
J " Remove the blank line
ò " Recursively...
yl " Yank the letter under the cursor
Àp " And paste it 'a' times
Àl " Move 'a' times to the right ('l' for right)
l " Move to the right one more time
" (implicit) end the loop
-
\$\begingroup\$
'l' for right... I'm guessing that's a holdover Vim thing? Otherwise ... why? \$\endgroup\$AdmBorkBork– AdmBorkBork2017年10月11日 19:51:16 +00:00Commented Oct 11, 2017 at 19:51 -
2\$\begingroup\$ @AdmBorkBork yeah
lis right in vim. it might be orthographically backwards, but it's geometrically correct:lis the rightmost letter key of the middle row. \$\endgroup\$Jonah– Jonah2017年10月12日 07:05:59 +00:00Commented Oct 12, 2017 at 7:05 -
\$\begingroup\$ @DJMcMayhem Right. I made it right. \$\endgroup\$Jonah– Jonah2017年10月12日 07:08:40 +00:00Commented Oct 12, 2017 at 7:08
Python 3, 58 bytes
Working on golfing it down.
I know there are already other Python answers, but I thought I'd post this one too seeing as it scores pretty well compared to the others, despite being a full function and not a lambda.
Takes input as function parameters, and prints to STDOUT.
def f(s,n,i=0):
for c in s:print(end=[c,c*n][i%n<1]);i+=1
For one byte less (57), I coded a lambda, however similar answers have already been posted by other users:
lambda s,n:''.join([c,c*n][i%n<1]for i,c in enumerate(s))
Brain-Flak (BrainHack), 122 + 3 (-A) = 125 bytes
I am sure this is too long, but I spent quite a while looking and couldn't find any improvements.
([]){{}([(([{}]<>)<{({}<<>(({})<>)>())}{}{}>)<{({}<<>({}<>)>())}{}>]<>)([][()])}({}{}<>){({}{(<()>)}{}[()])}{}{({}<>)<>}<>
05AB1E, (削除) 12 (削除ここまで) 11 bytes
×ばつ?
Explanation
v # for each letter in the input string
è # index into
X‚ # the list [input_int,1]
R # reversed
NIÖ # with letter_index % input_int == 0
×ばつ # repeat the current letter this many times
? # print
Mathematica, 71 bytes
""<>s[[i]]~t~If[i~Mod~#2==1,#2,1]~(t=Table)~{i,Tr[1^(s=Characters@#)]}&
saved -2 bytes by listening to user202729
-
\$\begingroup\$ I think
MapoverCharactersmay be shorter. \$\endgroup\$user202729– user2027292017年10月12日 10:03:27 +00:00Commented Oct 12, 2017 at 10:03 -
\$\begingroup\$ @user202729 ok! -2 bytes \$\endgroup\$ZaMoC– ZaMoC2017年10月12日 10:23:48 +00:00Commented Oct 12, 2017 at 10:23
K (oK), (削除) 23 (削除ここまで) 19 bytes
Solution:
{,/(1|y*~y!!#x)#'x}
Examples:
> {,/(1|y*~y!!#x)#'x}["Hello, World!";3]
"HHHellllo, Worrrld!!!"
> {,/(1|y*~y!!#x)#'x}["Code golf";1]
"Code golf"
> {,/(1|y*~y!!#x)#'x}["abcdefghijklm";10]
"aaaaaaaaaabcdefghijkkkkkkkkkklm"
Explanation:
{,/(1|y*~y!!#x)#'x} / the solution
{ } / lambda function with x and y as implicit parameters
( ) / do everything in brackets together
#x / count x, #"Hello, World!" -> 13
! / til, !13 -> 0 1 2 3 4 5 6 7 8 9 10 11 12
y! / y modulo, 3!0 1 2 3 4 5 6 7 8 9 10 11 12 -> 0 1 2 0 1 2 0 1 2 0 1 2 0
~ / not, ~0 1 2 0 1 2 0 1 2 0 1 2 0 -> 1 0 0 1 0 0 1 0 0 1 0 0 1
y* / multiply by y, 3*1 0 0 1 0 0 1 0 0 1 0 0 1 -> 3 0 0 3 0 0 3 0 0 3 0 0 3
1| / min of 1 and, 1|3 0 0 3 0 0 3 0 0 3 0 0 3 -> 3 1 1 3 1 1 3 1 1 3 1 1 3
#'x / take each parallel, 1 2 3#'"abc" -> "a", "bb", "ccc"
,/ / flatten the list, "a", "bb", "ccc" -> "abbccc"
Notes:
- -4 bytes with different approach
Excel VBA, 71 Bytes
Anonymous VBE immediate window function that take input from range [A1:B1] and outputs to the VBE immediate window.
For i=1To[Len(A1)]:[C1]=i:?[Rept(Mid(A1,C1,1),B1^(Mod(C1,B1)=1))];:Next
sas a character array? \$\endgroup\$s" <- is this a strict requirement (overwriting the original string) or ist it ok to just output the final result? \$\endgroup\$