Home‎ > ‎CS 11M‎ > ‎

Project 5: I Palindrome I

Introduction

In this project you will use both for and while loops. Give yourself time. This one is tricky!

    Do Code Lab #4 for practice. 

Part 1: Detecting a Palindrome

In the first part of the project you will write a for loop that detects a palindrome in a single word. Here's some starter code.

#include <ArduinoSTL.h>

using namespace std; 

void setup() {
  Serial.begin(9600);
  cout << "Enter a word and I will detect if it's a palindrome." << endl;
}

void loop() {
  string wrd; 
  cin >> wrd; 

  // do detection here.
}

In the detection you will need a for loop that loops over the letters in the word. Recall from the lesson that you can do that with the following for loop:

  for (int i = 0; i < wrd.length(); i++ ) {
    cout << "wrd[" << i << "] == " << wrd[i] << endl;
  }

Be sure you understand what the above loop does before you continue. 

Specifications

  1. Start with the starter code. 
  2. Name your program palindrome_word.ino
  3. Your program should ignore spaces. (i.e. don't change the cin line). 
  4. Do not process words that are less than two characters (e.g. "I" or "a"). 
  5. If you encounter a one character word you must print this exact message:

    All one letter words are palindromes.

  6. If the word is a palindrome you must print a message like this (assuming the input is "man"):

    The word "man" is NOT a palindrome.

  7. If the word is not a palindrome you must print a message like this (assuming the input is "mom"):

    The word "mom" is a palindrome.

Example Output

I
All one letter words are palindromes.
truck
The word "truck" is NOT a palindrome.
man
The word "man" is NOT a palindrome.
mom
The word "mom" is a palindrome.

The underlined text is the user's input. You will not see it in the serial monitor. 

Part 2: Whole Sentences 

For the second part of the assignment you will make your palindrome tester work on whole sentences. To do that you will have to wrap your existing code into a while loop. The loop should keep processing words until the whole processed sentence is a palindrome, then it should print the palindrome and start over. You might need to break out of building a sentence so that you can start over. To do that your sketch should read the LEFT button. If it is pushed restart the sentence. 

Specifications

  1. Start with your completed code from part 1. 
  2. Name your program palindrome_sentence.ino
  3. Read words using cin (the same way as the first part). 
  4. Ignore spaces and newlines while looking for palindromes. So you should detect a palindrome if the user types:

    man<Enter>o<Enter>nam

  5. If the LEFT button is pushed at any time you should print exactly these words and restart the detection. (It's okay to print the "Abort!" more than once).

    Abort!
     
  6. Sentences with one letter should not be considered palindromes. 
  7. Print a prompt every time you are ready for a new sentence. The prompt must be exactly these words: 

    Enter a sentence and I will detect if it's a palindrome.

  8. Do not print any output until the sentence is a palindrome or the LEFT button is pushed. 
  9. When you detect a palindrome print the following exact words:

    Palindrome detected!

  10. If the left button is pushed I will expect to see the prompt again (in addition to "Abort!").

Hints

Your code will need a while loop which should exit on two conditions:
  1. You've detected a palindrome. 
  2. The LEFT button has been pushed.
Consider making the loop condition a boolean variable like below:

  bool keep_going = true;
  while (keep_going) {

    // Program goes here.

  }

In order to properly detect the LEFT button you can't be waiting around for user input in cin.  Consider reading cin like this:

if (Serial.available()) {
  cin >> wrd;
}

Notice there is no cin.ignore(1). You only need that if you're reading numbers. When you read strings the '\n' character is consumed by cin. (I know, that's messed up). Last you will be collecting words into a sentence. Remember you can do this by concatenating strings. Consider the following operation:

sentence += word; 

Example Output

Enter a sentence and I will detect if it's a palindrome.
a man a plan a canal panama
Palindrome detected!
Enter a sentence and I will detect if it's a palindrome.
man o nam
Palindrome detected!
Enter a sentence and I will detect if it's a palindrome.
blah blah blah <LEFT-BUTTON>
Abort!

Extra Credit +5

Make your palindrome detector work properly without regard to capitalization. For example:

Enter a sentence and I will detect if it's a palindrome.
A man a plan a canal Panama
Palindrome detected!

Turn In

Turn in the following files:
  1. Class exercises:
    1. counting.ino
    2. fader.ino
    3. graph-fader.ino
    4. while-for-for.ino
  2. palindrome_word.ino (part 1)
  3. palindrome_sentence.ino (part 2) 
Submit your files on Canvas
Comments