C++ MyProgrammingLab problem A mirror string is a string whose mirror image is t
ID: 3562468 • Letter: C
Question
C++ MyProgrammingLab problem
A mirror string is a string whose mirror image is the same as the string (i.e., putting the string in a mirror, it reads the same). Mirror strings are made up ONLY of "mirror letters". These are the characters that are mirror symmetrical: i, l, o, u, v, w, x, I, M, O, T, U, V, W, X .
A string , s, is a mirror string if and only if:
Here are some examples of mirror strings : "toot", "x", "wow".
Write a recursive, bool-valued function, isMirrorString that accepts a string and returns true if and only if the string is a mirrorstring as defined above.
Do not write the entire program or create any other functions for this problem.
Explanation / Answer
//Tested here: http://ideone.com/pBmXas
//A mirror string is a string whose mirror image is the same as the string (i.e., putting the string in a mirror, it reads the same).
// Mirror strings are made up ONLY of "mirror letters". These are the characters that are mirror symmetrical: i, l, o, u, v, w, x, I, M, O, T, U, V, W, X .
// A string , s, is a mirror string if and only if:
// s is the empty string or s consists of a single mirror letter (which reads the same back or forward), or
// the first and last characters of s are the same and are mirror letters, and the rest of the string (i.e., the second through next-to-lastcharacters )
// form a mirror string .
//Write a recursive, bool-valued function, isMirrorString that accepts a string and returns true if and only if the string is a mirrorstring as defined above.
bool isMirrorString(string s)
{
if(s.length()==0 || (s.length()==1 && ((s[0]=='i') || (s[0]=='l') || (s[0]=='o') ||(s[0]=='u')|| (s[0]=='v') ||(s[0]=='w') ||(s[0]=='x') ||
(s[0]=='I') || (s[0]=='M') || (s[0]=='O') ||(s[0]=='T')|| (s[0]=='V') ||(s[0]=='W') ||(s[0]=='X')||(s[0]=='U'))))
return true;
else
{
return (s[0]==s[s.length()-1]) && isMirrorString(s.substr(1,s.length()-2));
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.