# How to select all occurrences of a specified word in a string and append another word to each of them?

When using infix-RPN package, we can use \infixtoRPN to convert infix expression to RPN expression. The output of the conversion can be retrieved by invoking \RPN.

\infixtoRPN{cos(x)+sin(x)}, for example, will yield x cos x sin add that is available in \RPN.

My question, how to insert RadtoDeg right after the x? So the final will be x RadtoDeg cos x RadtoDeg sin add.

The skeleton is given as follows.

\documentclass{article}
\usepackage{infix-RPN}

\def\f(#1){cos(#1)+sin(#1)}

% #1 target word
% #2 inserted word
\def\appendtoRPNafter#1#2{%
%implementation using \RPN goes here!
%don't forget to return the result!
}
\begin{document}
\infixtoRPN{\f(x)}%
\end{pspicture}
\end{document}

-

\documentclass{article}
\usepackage{infix-RPN}

\def\f(#1){cos(#1)+sin(#1)}

\infixtoRPN{\f(x)}%

\typeout{A: \RPN}

% #1 target word
% #2 inserted word
\def\appendtoRPNafter#1#2{%
\def\tmp##1 #1 ##2{##1\ifx\tmpb##2\else\space #1 #2 \expandafter\tmp\fi##2}%
\def\tmpb.{}%
\edef\RPN{\expandafter\tmp\RPN\tmpb. #1 \tmpb.}
}


A:  x cos x sin add