Question

I really need help with the code for this, in C++ please, I know there's a...

I really need help with the code for this, in C++ please, I know there's a similar question but the answer isn't correct :[ A palindrome is a string that reads the same forwards and backwards (ignoring spaces, punctuation, and capitalization). Examples are the familiar ``If I had a hi-fi,'' the grander ``A man, a plan, a canal, Panama,'' and ``Some men interpret nine memos.'' So the goal: Design, implement, document, and test a program that reads a line of input from the terminal window and reports whether or not that line is a palindrome. The object of this project is to review and exercise C++ in general and arrays in C++ in particular. Input: An input line may contain any characters, though only letters are relevant in palindromes. Upper-case and lower-case letters are considered the same; 'A' = 'a'. For convenience, the maximum length of the input is 80 characters. Output: The program will prompt for the input line---a candidate palindrome---and will report whether or not the line is a palindrome. Errors: The program may assume that the input is as described. It need not detect any errors. Example: Several runs of the program might look like this: csh> pal Enter a line that might be a palindrome: Madam I'm Adam. The string is a palindrome. csh> pal Enter a line that might be a palindrome: Able was I ere I saw Elba. The string is a palindrome. csh> pal Enter a line that might be a palindrome: Go hang a salami, I'm a lasagna hog. The string is a palindrome. csh> pal Enter a line that might be a palindrome: This is another candidate string. The string is NOT a palindrome. PLEASE INCLUDE/ REQUIRED TO BE A COMPLETE ANSWER: Use arrays of characters to hold any strings that the program manipulates, and terminate strings within those arrays with the null character '\0'. Include at least one function. Helpful direction/ tips: Read the input one character at a time and preserve only the letters; they're all that's interesting. Note the iostream.h function get( ), which reads exactly one character, including the blank. The following loop reads all the characters in an input line, one at a time, into the variable ch. char ch; . . . cin.get(ch); while ( ch >= ' ' ) { < Do something with ch. > cin.get(ch); } When designing tests for your program, note that a string that contains no letters is a palindrome, as is an empty string.

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

If you have any doubts, please give me comment...

#include <iostream>

#include <cctype>

using namespace std;

#define MAX_LINE 80

int main()

{

char str[MAX_LINE];

char ch;

int n = 0, i;

cout<<"Enter a line that might be a palindrome:"<<endl;

cin.get(ch);

while (ch >= ' ')

{

if (isalpha(ch))

{

str[n] = tolower(ch);

n++;

}

cin.get(ch);

}

str[n] = '\0';

bool isPal = true;

while (i < n / 2)

{

if (str[i] != str[n - i - 1])

{

isPal = false;

break;

}

i++;

}

if (isPal)

cout << "The string is a palindrome." << endl;

else

cout << "The string is NOT a palindrome." << endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
I really need help with the code for this, in C++ please, I know there's a...
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
  • 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...

  • Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward...

    Palindrome Detector C++ A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for...

  • please I need help with the question: Problem: Write a C++ program that reads each character...

    please I need help with the question: Problem: Write a C++ program that reads each character from a user input, extracts the character if it is a digit, and calculates the sum of all the digits. The program stops reading the user input when the new line character ‘\n’ is encountered. For the output of the program, you should print each digit extracted from the user input and the sum of all the digits. (*The main difference between “cin >>...

  • Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a str...

    Please Code in Java and follow the directions given Thanks Program required Directions .A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right. Examples: 3 a) radar à is a palindrome b)Able was I ere I saw Elba à is a palindrome e good à not a palindrome Write java program to read a line of text and tell...

  • A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here...

    A palindrome is any word, phrase, or sentence that reads the same forward and backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a program that determine whether an input string is a palindrome or not. Input: The program should prompt the user "Please enter a string to test for palindrome or type QUIT to exit: " and then wait for the user input....

  • #19 with the following instructions 19. A palindrome is a string that can be read backward...

    #19 with the following instructions 19. A palindrome is a string that can be read backward and forward with the same result. For example, the following is a palindrome: Able was I ere I saw Elba. unction to test if a string is a palindrome using a stack. You can push characters in the stack one by one. When you reach the en string, you can pop the characters and form a new string. If the two strings are exactly...

  • A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks...

    A palindrome is a word or phrase that reads the same forward and backward, ignoring blanks and considering uppercase and lowercase versions of the same letter to be equal. For example, the following are palindromes: • warts n straw • radar • Able was I ere I saw Elba • tacocat Write a program that will accept a sequence of characters terminated by a period and will decide whether the string—without the period—is a palindrome. You may assume that the...

  • Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include...

    Please use Visual Studio! Let me know if you need anything else <3 #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. //...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • I need help programming this program in C. 1.) Write a program that reads a message,...

    I need help programming this program in C. 1.) Write a program that reads a message, then checks whether it's a palindrome (the letters in the message are the same from left to right as from right to left), example is shown below: Enter a message: He lived as a devil, eh? Palindrome Enter a message: Madam, I am Adam. Not a Palindrome 2.) Ignore all characters that aren't letters. Use integer variables to keep track of positions in the...

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