add tag
samcarter
> This is part of the Summer of Code 2022 series, see https://topanswers.xyz/tex?q=2059 for more information

Let's duplicate/triplicate/n-plicate letters!

If you get an input sentence like 

```
wow
```

```
Ducks are cute
```

```
Quack
```

repeat each character (excluding spaces) n times. The results should for example be

```
wwwooowww
```

```
DDuucckkss aarree ccuuttee
```

or you can repeat the first character 1 time, the second 2 times etc:

```
Quuaaacccckkkkk
```


![SoC.png](/image?hash=fb41e3f94b678a2fbc5c5974f5cbf8c269b117136c3c9f71d1a975416a425d88)
Top Answer
frougon
Here is one MWE for each macro so as to make them easy to read.

I use recursion and `\tl_if_head_is_space:nTF` in order to obtain [expandable macros](https://topanswers.xyz/tex?q=2007#a2255) (`\peek_after:Nw` and its friends such as `\peek_catcode:NTF` are unfortunately not expandable).

# Same repeat count for each non-space token

```
\documentclass{article}

\ExplSyntaxOn

% cf. “Head and tail of token lists” in interface3.pdf
% (alternate definition from Skillmon's comment here)
\use:n { \cs_new:Npn \__socii_gobble_space:w } ~ { }

\cs_new:Npn \socii_repl:nn #1#2
  {
    \tl_if_head_is_space:nTF {#2}
      { ~ \socii_repl:no {#1} { \__socii_gobble_space:w #2 } }
      {
        \tl_if_empty:nF {#2}
          {
            \prg_replicate:ne {#1} { \tl_head:n {#2} }
            \socii_repl:ne {#1} { \tl_tail:n {#2} }
          }
      }
  }

\cs_generate_variant:Nn \prg_replicate:nn { ne }
\cs_generate_variant:Nn \socii_repl:nn { no, ne }

\cs_new_eq:NN \repeatLettersNTimes \socii_repl:nn

\ExplSyntaxOff

\begin{document}

\repeatLettersNTimes{3}{wow}

\repeatLettersNTimes{2}{Ducks are cute}

\edef\tmp{\repeatLettersNTimes{4}{This is expandable.}}
\texttt{\meaning\tmp}

\end{document}
```

![image.png](/image?hash=4725052d686f1a6bade9ad4b63a41ef2019f7d60ce8057b496fb44733c948606)

# Repeat count increasing from 1

```
\documentclass{article}

\ExplSyntaxOn

% cf. “Head and tail of token lists” in interface3.pdf
% (alternate definition from Skillmon's comment here)
\use:n { \cs_new:Npn \__socii_gobble_space:w } ~ { }

% #1: initial repeat count
% #2: string to process
\cs_new:Npn \__socii_repl_progressive:nn #1#2
  {
    \tl_if_head_is_space:nTF {#2}
      { ~ \__socii_repl_progressive:no {#1} { \__socii_gobble_space:w #2 } }
      {
        \tl_if_empty:nF {#2}
          {
            \prg_replicate:ne {#1} { \tl_head:n {#2} }
            \__socii_repl_progressive:ee { \int_eval:n { #1+1 } }
              { \tl_tail:n {#2} }
          }
      }
  }

\cs_generate_variant:Nn \prg_replicate:nn { ne }
\cs_generate_variant:Nn \__socii_repl_progressive:nn { no, ee }

% This macro behaves as is if took one 'n'-type argument, hence the signature.
\cs_new:Npn \socii_repl_progressive:n
  {
    \__socii_repl_progressive:nn { 1 }
  }

\cs_new_eq:NN \repeatLettersProgressive \socii_repl_progressive:n

\ExplSyntaxOff

\begin{document}

\repeatLettersProgressive{wow}

\repeatLettersProgressive{Ducks are cute}

\edef\tmp{\repeatLettersProgressive{This is expandable.}}
\texttt{\tiny\meaning\tmp}

\end{document}
```

![image.png](/image?hash=790d65c0462ec52f7616e793faa4d4c9b682f89281bf615b166ceb360d8834c3)
Answer #2
Skillmon
A strip of solutions:

The following implements the macro `\replicator` that accepts an optional argument specifying the number of times an item should be replicated. If it is omitted the number of replications grows (starting at 1). Spaces aren't duplicated, multiple consecutive spaces are truncated into a single space.

It borrows some code and ideas from `expl3`, being `\tl_if_head_is_space:nT` and code based on `\prg_replicate:nn`, though changing it a bit to use `\unexpanded` instead of `f`-type expansion.

`\replicator` should work with arbitrary valid `n`-type input and expands in exactly two steps of expansion, leaving its result inside `\unexpanded` so that it doesn't expand further in `e`- or `x`-type expansion.

```
\documentclass{article}

\usepackage{xcolor,clrstrip}% output thingy
\usepackage{expkv}% for \ekvoptargTF and \ekverr

\makeatletter
% stealing the \tl_if_head_is_space:nT test from `expl3`
\ExplSyntaxOn
\cs_new_eq:NN \replicator@ifheadspace \tl_if_head_is_space:nT
\ExplSyntaxOff

\def\replicator
  {%
    \unexpanded\expanded{{\iffalse}}\fi
    \ekvoptargTF
      {\replicator@collect\replicator@const}%
      {\replicator@collect\replicator@grow\@ne}%
  }
\long\def\replicator@collect#1#2#3%
  {\expandafter#1\the\numexpr#2\relax;#3{}\replicator@end}
\long\def\replicator@const#1;#2#3\replicator@end
  {%
    \replicator@replicate{#1}{#2}%
    \if\relax\detokenize{#3}\relax\replicator@end\fi
    \replicator@ifheadspace{#3}{ }%
    \replicator@const#1;#3\replicator@end
  }
\long\def\replicator@grow#1;#2#3\replicator@end
  {%
    \replicator@replicate{#1}{#2}%
    \if\relax\detokenize{#3}\relax\replicator@end\fi
    \replicator@ifheadspace{#3}{ }%
    \expandafter\replicator@grow\the\numexpr#1+1\relax;#3\replicator@end
  }
\long\def\replicator@end\fi#1\replicator@end{\fi\iffalse{{\fi}}}

% based on \prg_replicate:nn of `expl3`
\long\def\replicator@replicate#1#2%
  {%
    \iffalse{\fi\replicator@replicate@first#1\endcsname{#2}}%
  }
\def\replicator@replicate@first#1%
  {\csname replicator@replicate@first@#1\replicator@replicate@@}
\def\replicator@replicate@@#1%
  {\csname replicator@replicate@#1\replicator@replicate@@}
\long\def\replicator@replicate@#1{\endcsname}
\long\expandafter\def\csname replicator@replicate@0\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}}
\long\expandafter\def\csname replicator@replicate@1\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1}
\long\expandafter\def\csname replicator@replicate@2\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1}
\long\expandafter\def\csname replicator@replicate@3\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1}
\long\expandafter\def\csname replicator@replicate@4\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@5\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@6\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@7\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@8\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@9\endcsname#1%
  {\endcsname{#1#1#1#1#1#1#1#1#1#1}#1#1#1#1#1#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@first@-\endcsname#1%
  {%
    \ekverr{replicator}{negative replication}%
    \unexpanded\expandafter{\iffalse}\fi
  }
\long\expandafter\def\csname replicator@replicate@first@0\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi}
\long\expandafter\def\csname replicator@replicate@first@1\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi#1}
\long\expandafter\def\csname replicator@replicate@first@2\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi#1#1}
\long\expandafter\def\csname replicator@replicate@first@3\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi#1#1#1}
\long\expandafter\def\csname replicator@replicate@first@4\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@first@5\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi#1#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@first@6\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi#1#1#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@first@7\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi#1#1#1#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@first@8\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi#1#1#1#1#1#1#1#1}
\long\expandafter\def\csname replicator@replicate@first@9\endcsname#1%
  {\unexpanded\expandafter{\iffalse}\fi#1#1#1#1#1#1#1#1#1}

\def\solveSoC
  {%
    \begin{colorstrip}{green!45}
      \replicator[3]{wow}\par\noindent
      \replicator[2]{Ducks are cute}\par\noindent
      \replicator{Quack}
    \end{colorstrip}
  }

\begin{document}
\solveSoC
\end{document}
```

![soc2-duplicatestring-1.png](/image?hash=9cb77a628b68bbf965525367d1862d965050cecba134742942eff7e3d52bc6fa)
Answer #3
samcarter
*no spoiler*

Discovery of the day is `\tl_map_inline`. 


```
\documentclass{article}

\pagestyle{empty}

\begin{document}

Let’s duplicate/triplicate/n-plicate letters!

If you get an input sentence like

\begin{verbatim}
wow
\end{verbatim}
\begin{verbatim}
Ducks are cute
\end{verbatim}
\begin{verbatim}
Quack
\end{verbatim}
repeat each character (excluding spaces) n times. The results should for example be
\begin{verbatim}
wwwooowww
\end{verbatim}
\begin{verbatim}
DDuucckkss aarree ccuuttee
\end{verbatim}
or you can repeat the first character 1 time, the second 2 times etc:
\begin{verbatim}
Quuaaacccckkkkk
\end{verbatim}

\ExplSyntaxOn

\str_new:N \l_sam_a_str
\str_set:Nn \l_sam_a_str { wow } 

\str_new:N \l_sam_b_str
\str_set:Nn \l_sam_b_str { Ducks~are~cute } 

\str_new:N \l_sam_c_str
\str_set:Nn \l_sam_c_str { Quack } 

\int_new:N \l_sam_count_int

\cs_new_protected:Npn \sam_repeatletters:nN #1#2 
{
  \str_map_inline:Nn #2 
  { 
    \str_if_eq:nnTF { ##1 } { ~ } { \space }
    {
      \int_step_inline:nn {#1}
      {
        ##1
      }
    }
  }
}

\cs_new_protected:Npn \sam_repeatcrazy:N #1 
{
  \int_zero:N \l_sam_count_int
  \str_map_inline:Nn #1 
  { 
    \str_if_eq:nnTF { ##1 } { ~ } { ~ }
    {
      \int_incr:N \l_sam_count_int
      \int_step_inline:nn { \l_sam_count_int }
      {
        ##1
      }
    }
  }
}

\par \sam_repeatletters:nN {3} \l_sam_a_str 
\par \sam_repeatletters:nN {2} \l_sam_b_str 
\par \sam_repeatcrazy:N \l_sam_c_str 
\par \sam_repeatcrazy:N \l_sam_b_str

\ExplSyntaxOff

\end{document}
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.