Question

In: Computer Science

c++: I need #include <iostream> header A palindrome (Links to an external site.) is a word,...

c++:

I need #include <iostream> header

A palindrome (Links to an external site.) is a word, phrase, number, or sequence of words that reads the same backward as forward. Punctuation, capitalization, and spaces between the words or lettering are allowed.

Here are some examples of word and phrase palindromes.

Words:

  • Civic

  • Kayak

  • Level

  • Madam

  • Mom

  • Noon

  • Racecar

  • Radar

  • Refer

  • Rotor

Phrases:

  • Don't nod.

  • I did, did I?

  • My gym

  • Step on no pets

  • Top spot

  • Never odd or even
  • Eva, can I see bees in a cave?

  • No lemon, no melon

Your assignment is to create a function that tests whether a string is a palindrome or not.

bool isPalindrome(string phrase)

Your main function should consist of a while loop that calls isPalindrome() until the user exits the program with a Control-D or other interrupt indicator. Your output should prompt for a candidate palindrome and reprint the phrase and let the user know whether the phrase meets the criteria or not. Be sure your output demonstrates various test cases including invalid input.

Hints:

You may want to remove spaces, punctuation, and spaces, then reverse the order of the phrase you are testing, then compare the reversed phrase with the original, to see if it matches.

  1. The regular expression (regex), below, will remove spaces, punctuation, etc.
regex regexp("[^a-zA-z]");
string result = regex_replace(phrase, regexp,"");  // remove spaces, punctuation, etc. 

2. You should change the phrase to lower case before you do your comparison. There are several ways to do this. I used the transform, and ::tolower() to accomplish this. You can read up on this option.  

3. The standard library has a reverse function that you can use out of the box, but it reverses the original string. Therefore, before reversing you will want to make a copy of the original and compare the copy to the original. You can use copy() for that purpose. Again, look up and study this function.  

4. There is a method in the string library to compare strings to see if two strings are equal or not. Read up on compare() too.

5. You can optionally use shorthand conditional syntax to print output "true" or "false" in your test results as follows:

(isPalindrome("Sit on a potato pan, Otis") ? "true" : "false")

Solutions

Expert Solution

Code:

#include <iostream>

#include<string.h>

using namespace std;

bool isPalindrome(string str)

{

int l=0,h=str.length();

for (int i=0;i<h;i++) // convert to lowercase string

str[i] = tolower(str[i]);

while (l <= h) {

if (!(str[l] >= 'a' && str[l] <= 'z')) // remove other symbol in left of string

l++;

else if (!(str[h] >= 'a' && str[h] <= 'z')) //remove other symbols in right of string

h--;

else if (str[l] == str[h]) // If characters are equal

l++, h--;

else

return false; // If characters are not equal then not palindrome

}

return true; // Returns true if palindrome

}

int main()

{

string str;

char ch;

while(1)

{

str.clear();

fflush(stdin);

cout<<"Enter string: ";

getline (cin, str);

if (isPalindrome(str))

cout <<"The string is palindrome\n";

else

cout << "The string is not palindrome\n";

cout<<"Do you want to enter another string?(y/n)\n";

cin>>ch;

if(ch!='y')

exit(0);

}

return 0;

}

Output:


Related Solutions

Go to the Bureau of Economic Analysis (Links to an external site.)Links to an external site....
Go to the Bureau of Economic Analysis (Links to an external site.)Links to an external site. (BEA) website and look at quarterly data from the last few years of the National Accounts. Can you make a decision of what part of the business cycle the U.S. economy is currently in? Why? What factors lead you to this conclusion? You may want to do additional research of sources to reach a conclusion. If so, please identify the sources that added to...
Visit the Bureau of Labor Statistics (Links to an external site.)Links to an external site. website...
Visit the Bureau of Labor Statistics (Links to an external site.)Links to an external site. website and explore the Consumer Price Index section. Read the "Current CPI Economic News Releases" and summarize the latest release. Did the CPI increase or decrease? What caused the index to increase or decrease? Explain. Visit the inflation calculator (Links to an external site.)Links to an external site. section and easily find out how the buying power of the dollar has changed over the years....
Watch the Shifts in Aggregate Demand video (Links to an external site.)Links to an external site....
Watch the Shifts in Aggregate Demand video (Links to an external site.)Links to an external site. (embedded in the reading) and summarize the factors that cause the AD curve to shift. Did the video help you understand the model? Pick one of the four scenarios listed below the video and explain in your own words which way the AD curve will shift. At least 200 words
INSTRUCTIONS Watch the Starbucks (Links to an external site.)Links to an external site. video and answer...
INSTRUCTIONS Watch the Starbucks (Links to an external site.)Links to an external site. video and answer the question below. How has leadership impacted Starbucks' current growth and how will their leaders need to evolve in order to maintain the company's success given the threats to the specialty coffee industry? in 300 word
using the Oyez audio file and transcript (Links to an external site.)Links to an external site....
using the Oyez audio file and transcript (Links to an external site.)Links to an external site. (go to "Opinion Announcement," which is a seven minute audio clip on the court's decision on the Snyder v. Phelps case), briefly summarize the facts of the Snyder v. Phelps (2011) case. By using facts presented, clearly explain why the Supreme Court ruled in favor of the Westboro Baptist Church. Then, tell us why you agree/disagree with the Court’s decision.
Again, please use www.federalreserve.gov (Links to an external site.)Links to an external site. and find what...
Again, please use www.federalreserve.gov (Links to an external site.)Links to an external site. and find what the current Fed policy is for interest rates. Do you think this policy is appropriate for the economy? Why or why not?
Centers for Disease Control and Prevention. (2017). Stroke(Links to an external site.)Links to an external site....
Centers for Disease Control and Prevention. (2017). Stroke(Links to an external site.)Links to an external site. (Links to an external site.)Links to an external site.. Retrieved from http://www.cdc.gov/stroke/index.htm Initial Discussion Post: Compose a response to the following questions: As an RN in an emergency room what signs and symptoms would you associate with someone having a stroke? Would these symptoms be different in different cultures and genders? Identify one culture or gender and discuss their risk factors and presentation of...
1. go to www.sec.gov (Links to an external site.)Links to an external site. 2. Hover your...
1. go to www.sec.gov (Links to an external site.)Links to an external site. 2. Hover your cursor over "Filings" in the menu bar across the top 3. click on "company filings search" that pops up when you hover 4. in the FAST SEARCH box on the far right type in "AEO" for the ticker and click "search" for American Eagle. For Buckle type in "BKE". 5. in the "filings" column on the far left look for the most recent "10-K"...
Edmunson Electrical Distribution (www.edmunson-electrical.co.uk (Links to an external site.)Links to an external site.) is a leading...
Edmunson Electrical Distribution (www.edmunson-electrical.co.uk (Links to an external site.)Links to an external site.) is a leading distributor of electrical equipment and components with over 230 branches in the United Kingdom. The company is a wholesaler of electrical products acting as an intermediary between manufacturers and customers. Accounts are classified according to turnover and margins achieved. The ‘bread and butter’ of the business is the electrical contractor, who provides high turnover but low margins. The more significant the purchases, the higher...
This is a statistics calculator and simulator (Links to an external site.). For this assignment, I...
This is a statistics calculator and simulator (Links to an external site.). For this assignment, I would like you to start with a real life scenario in which you might need to know some statistical measures. Provide a real life scenario (you must have 10 numbers with a frequency of 1 and calculate the statistics) Take a screenshot of the completed stats. Now, change your frequencies to anything you want and see how the data changes. Take a screen shot...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT