Question

In C++, create a recursive bool function which determines if a string is symmetrical or not....

In C++, create a recursive bool function which determines if a string is symmetrical or not. A symmetrical string can be read the same front to back (ex. kayak).

Function Blueprint

---------------------

bool isSymmetrical(string s)

if (s is the empty string or s is of length 1)

return true

else if (s's first and last characters are the same letter)

  return isSymmetrical (s minus its first and last characters)

else
return false

0 0
Add a comment Improve this question Transcribed image text
Answer #1

recursive function

bool isSymmetrical(char string[])
{
   int n;
   for(n=0;string[n]!='\0';n++);//find the length of the string
     
   static int lowerIndex=0,upperIndex=n-1;
//condition for the string having single character
if (lowerIndex == upperIndex)
{
return true;
}
//if any mismatch found then return false
//first character and last character will compare
if (string[lowerIndex] != string[upperIndex])
{
return false; //return false
}
//if no mismatch is found then recursively call to the method
if (lowerIndex < upperIndex+1)
{
   lowerIndex= lowerIndex+1; //increase the value of lowerIndex
   upperIndex = upperIndex-1; //decrease the value of upperIndex
return isSymmetrical(string); //call recursively()
}
//if all the characters match then return true
return true;
}

PROGRAMMING IMPLEMENTATION

#include <iostream>

using namespace std;
bool isSymmetrical(char string[])
{
   int n;
   for(n=0;string[n]!='\0';n++);//find the length of the string
     
   static int lowerIndex=0,upperIndex=n-1;
//condition for the string having single character
if (lowerIndex == upperIndex)
{
return true;
}
//if any mismatch found then return false
//first character and last character will compare
if (string[lowerIndex] != string[upperIndex])
{
return false; //return false
}
//if no mismatch is found then recursively call to the method
if (lowerIndex < upperIndex+1)
{
   lowerIndex= lowerIndex+1; //increase the value of lowerIndex
   upperIndex = upperIndex-1; //decrease the value of upperIndex
return isSymmetrical(string); //call recursively()
}
//if all the characters match then return true
return true;
}

// Driver Program
int main()
{
char string[100];
   cout<<endl<<"Enter the string";
   gets(string);//read the string
if ( isSymmetrical(string)) //condition for symmetric string
cout<<string<<" is symmetric"<<endl;
else
cout<<string<<" is not symmetric"<<endl;
return 0;
}

output

Add a comment
Know the answer?
Add Answer to:
In C++, create a recursive bool function which determines if a string is symmetrical or not....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName)...

    ***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...

  • In java, write a program with a recursive method which asks the user for a text...

    In java, write a program with a recursive method which asks the user for a text file (verifying that the text file exists and is readable) and opens the file and for each word in the file determines if the word only contains characters and determines if the word is alpha opposite. Now what I mean by alpha opposite is that each letter is the word is opposite from the other letter in the alphabet. For example take the word...

  • Bet you can't figure this out Create a Recursive Method to test whether a partial string...

    Bet you can't figure this out Create a Recursive Method to test whether a partial string is a subset of a full string. If the partial string is a subset of the full string return true. Otherwise return false.Create a Recursive Method to test whether a partial string is a subset of a full string. If the partial string is a subset of the full string return true. Otherwise return false. Here is the Main and Recursive below: (Note only...

  • Fix the function so that it checks if the string is a palindrome #include <iostream> using...

    Fix the function so that it checks if the string is a palindrome #include <iostream> using namespace std; //Fix the function so that it checks if the string is a palindrome //(same forwards as backwards ex: racecar / radar) bool is_palindrome(string str, int i){ //base cases if (/* add the condition */) return false;    if (/* add the condition */) return true;    //recursive call return is_palindrome(str, i-1); } int main(){ string x; cin >> x;    if (is_palindrome(x,x.length())){...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

  • 1.) Write a recursive function named isPalindrome that will take a single string as an argument...

    1.) Write a recursive function named isPalindrome that will take a single string as an argument and determine if the argument is a palindrome. The function must return True if the argument is a palindrome, False otherwise. Recall that any string is a palindrome if its first and last characters are the same and the rest of it is also a palindrome (therefore, a single character is a palindrome): 2.) Consider a text file named samplestrings.txt that contains a collection...

  • For this assignment, you will create a program that tests a string to see if it...

    For this assignment, you will create a program that tests a string to see if it is a palindrome. A palindrome is a string such as “madam”, “radar”, “dad”, and “I”, that reads the same forwards and backwards. The empty string is regarded as a palindrome. Write a recursive function: bool isPalindrome(string str, int lower, int upper) that returns true if and only if the part of the string str in positions lower through upper (inclusive at both ends) is...

  • Write a function check palindrome, which takes a string x as argument and which returns True...

    Write a function check palindrome, which takes a string x as argument and which returns True if x is a palindrome, and False otherwise. A palindrome is a word that reads the same backwards as forwards (like for example “racecar”). Your function should be recursive (i.e. call itself) and proceed as follows. 1. If x is a string of length 0 or 1 return True. 2. If the rst and the last letter of x are di erent, return False....

  • Create a C# Console Application named Front that creates a new string from a user's entry....

    Create a C# Console Application named Front that creates a new string from a user's entry. Take the first 3 characters of the given string and return the string with the 3 characters added at both the front and back. If the given string length is less than 3 do not accept the string

  • 1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true...

    1. DOES A DIGIT APPEAR IN AN INTEGER, Write a recursive function appears(n,i) that returns true if the digit i appears in the positive (base 10) integer n, false if it does not in c++. ex. Enter a positive integer: 54321 Enter a digit: 7 The digit 7 does NOT appear in the integer 54321. 2. IS AN INPUT STRING A PALINDROME? A palindrome is a string in which the letters are the same in both directions, disregarding capitalization and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT