I have two strings of equal lengths, let's say abc
and def
(in reality they can be much longer). Both strings consist only of letters a-z
, A-Z
(no numbers, no special characters etc.). I need to extract out the characters individually from both strings, and use them pairwise in another macro that I have defined.
For e.g. For the two strings abc
and def
and some other macro \printchar[2]{#1,#2}
that I have defined, I want to be able to pass the characters pair by pair into \printchar
, that is,
\printchar{a}{d}
\printchar{b}{e}
\printchar{c}{f}
in perhaps, a for loop of some kind. So I guess the question is two-fold:
- How to extract out characters from two input strings of equal lengths, character by character, and
- Subsequently use them, pairwise, inside a macro. (and it should loop through all characters in the string).
Pseudo-Code:
\documentclass[]{article}
\newcommand\printchar[2]{#1,#2}
\newcommand\foo[2]{% Takes in two strings
% <begin loop>
% Extracts out one character from both strings
\printchar{#1}{#2}%
% Continue loop until strings run out of characters
% <end loop>
}
\begin{document}
\foo{abc}{def} % Should print a,d,b,e,f
\end{document}
From some searching around, I think maybe the xstring
package can do the first part? But I'm not too sure how to integrate it with a for loop to achieve what I want.
%
and{
?