Docsity

CS 1410 Programming Assignment #5: Palindrome Programs, Assignments of Computer Science

Toggle sidebar
bg1
Page 1 of 2
CS 1410 Programming Assignment #5
String Objects and C-Strings
Palindrome Programs
Assignment
A palindrome is a string of letters that reads the same forward and backward (ignoring spaces,
punctuation, and capitalization). For example:
A man, a plan, a canal: Panama!
Write two (2) C++ programs to determine if a string represents a palindrome. The first program is
based on string objects while the second is based on cstrings.
Program #1 (named palindrome.cpp)
1. Prompt for the user to enter a potential palindrome
2. Read the user input in to a string object (the test input to your program may contain spaces but
will not contain punctuation or mixed-case letters)
3. Write a message stating that the input was or was not a valid palindrome
Suggestions
It is probably easiest to first remove all of the spaces from the string and to then do a character-by-
character comparison in a for-loop.
The following code fragment (which you may use in your program) will remove the spaces from a string
object (see p. 307):
for (int i = 0; i < s.length(); i++)
if (s[i] == ' ') // single space
s.erase(i, 1);
See palnumber (written in class) for an example of doing a character-by-character comparison of string
objects.
Although much of the code for this assignment is provided for you, you should understand what the
code is doing (who knows what could be on the next mid-term :-)
pf2

Partial preview of the text

Download CS 1410 Programming Assignment #5: Palindrome Programs and more Assignments Computer Science in PDF only on Docsity!

Page 1 of 2

CS 1410 Programming Assignment

String Objects and C-Strings

Palindrome Programs

Assignment

A palindrome is a string of letters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization). For example:

A man, a plan, a canal: Panama!

Write two (2) C++ programs to determine if a string represents a palindrome. The first program is based on string objects while the second is based on cstrings.

Program #1 (named palindrome.cpp)

  1. Prompt for the user to enter a potential palindrome
  2. Read the user input in to a string object (the test input to your program may contain spaces but will not contain punctuation or mixed-case letters)
  3. Write a message stating that the input was or was not a valid palindrome

Suggestions

It is probably easiest to first remove all of the spaces from the string and to then do a character-by- character comparison in a for-loop.

The following code fragment (which you may use in your program) will remove the spaces from a string object (see p. 307):

for (int i = 0; i < s.length(); i++) if (s[i] == ' ') // single space s.erase(i, 1);

See palnumber (written in class) for an example of doing a character-by-character comparison of string objects.

Although much of the code for this assignment is provided for you, you should understand what the code is doing (who knows what could be on the next mid-term :-)

Page 2 of 2

Program #2 (named cpalindrome.cpp)

  1. The program accepts a potential palindrome on the command line (i.e., an array of cstrings) For example: c:> cpalindrome a man a plan a canal panama
  2. Write a message stating that the input was or was not a valid palindrome

Suggestions

Each word entered on the command line is a space-separated string of characters, which the system provides to your program as a cstring located in the argv array. (Note that the following is not code that you write but is an illustration that corresponds to the input illustrated in step 1 above.) In this example, argc = 8 argv[0] = cpalindrome (the program name – often not of interest) argv[1] = a argv[2] = man argv[3] = a argv[4] = plan argv[5] = a argv[6] = canal argv[7] = panama

The test input will not contain punctuation marks or mixed case letters

Concatenate the command line arguments into a single cstring, which can then be checked as above char palindrome[1000] = “”; // empty string for (int i = 1; i < argc; i++) strcat(palindrome, argv[i]);

Submitting Your Code

Upload two files to WSU Online: palindrome.cpp and cpalindrome.cpp (please do not zip the files).

Testing

The test bed program is provided with the assignment.

  • Copy the file grade5.bat into the same directory as your source files.
  • From the command line, type grade5 and press enter. (The “Command Prompt” window must opened or configured so that the Visual Studio environment is properly initialized.)
  • Both programs should compile and produce several messages without additional user interaction. If it doesn’t work for you, it won’t work for me either.