32 lines
3.5 KiB
Plaintext
32 lines
3.5 KiB
Plaintext
\begin{Verbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}]
|
|
\PYG{c+c1}{// class to implement method 4 }
|
|
\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{class} \PYG{n+nc}{RecursiveReverse}\PYG{+w}{ }\PYG{k+kd}{implements}\PYG{+w}{ }\PYG{n}{PalindromeChecker}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
|
|
\PYG{+w}{ }\PYG{c+c1}{// comparing the String reversed using recursion to the original String (essentially method 1 but with recursion)}
|
|
\PYG{+w}{ }\PYG{n+nd}{@Override}\PYG{+w}{ }
|
|
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kt}{boolean}\PYG{+w}{ }\PYG{n+nf}{checkPalindrome}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
|
|
\PYG{+w}{ }\PYG{c+c1}{// returning true if the original String is equal to the reversed String, false if not}
|
|
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]++}\PYG{p}{;}\PYG{+w}{ }
|
|
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{equals}\PYG{p}{(}\PYG{n}{reverse}\PYG{p}{(}\PYG{n}{str}\PYG{p}{));}
|
|
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
|
|
|
|
\PYG{+w}{ }\PYG{c+c1}{// method to reverse the characters in a String using recursion }
|
|
\PYG{+w}{ }\PYG{k+kd}{public}\PYG{+w}{ }\PYG{k+kd}{static}\PYG{+w}{ }\PYG{n}{String}\PYG{+w}{ }\PYG{n+nf}{reverse}\PYG{p}{(}\PYG{n}{String}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
|
|
\PYG{+w}{ }\PYG{c+c1}{// base case - returning an empty String if there is no character left in the String}
|
|
\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]++}\PYG{p}{;}
|
|
\PYG{+w}{ }\PYG{k}{if}\PYG{+w}{ }\PYG{p}{(}\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{length}\PYG{p}{()}\PYG{+w}{ }\PYG{o}{==}\PYG{+w}{ }\PYG{l+m+mi}{0}\PYG{p}{)}\PYG{+w}{ }\PYG{p}{\PYGZob{}}\PYG{+w}{ }
|
|
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{l+s}{\PYGZdq{}\PYGZdq{}}\PYG{p}{;}
|
|
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
|
|
\PYG{+w}{ }\PYG{k}{else}\PYG{+w}{ }\PYG{p}{\PYGZob{}}
|
|
\PYG{+w}{ }\PYG{k+kt}{char}\PYG{+w}{ }\PYG{n}{firstChar}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{charAt}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}
|
|
\PYG{+w}{ }\PYG{n}{String}\PYG{+w}{ }\PYG{n}{remainder}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{str}\PYG{p}{.}\PYG{n+na}{substring}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]}\PYG{+w}{ }\PYG{o}{+=}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{l+m+mi}{1}\PYG{p}{;}\PYG{+w}{ }\PYG{c+c1}{// selecting the rest of the String, excluding the 0th character}
|
|
|
|
\PYG{+w}{ }\PYG{c+c1}{// recursing with what's left of the String}
|
|
\PYG{+w}{ }\PYG{n}{String}\PYG{+w}{ }\PYG{n}{reversedRemainder}\PYG{+w}{ }\PYG{o}{=}\PYG{+w}{ }\PYG{n}{reverse}\PYG{p}{(}\PYG{n}{remainder}\PYG{p}{);}\PYG{+w}{ }\PYG{n}{NewPalindrome}\PYG{p}{.}\PYG{n+na}{operations}\PYG{o}{[}\PYG{l+m+mi}{3}\PYG{o}{]++}\PYG{p}{;}
|
|
|
|
\PYG{+w}{ }\PYG{c+c1}{// returning the reversed rest of String with the first character of the String appended}
|
|
\PYG{+w}{ }\PYG{k}{return}\PYG{+w}{ }\PYG{n}{reversedRemainder}\PYG{+w}{ }\PYG{o}{+}\PYG{+w}{ }\PYG{n}{firstChar}\PYG{p}{;}
|
|
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
|
|
\PYG{+w}{ }\PYG{p}{\PYGZcb{}}
|
|
\PYG{p}{\PYGZcb{}}
|
|
\end{Verbatim}
|