Question
Program 2: Palindromes With C-Strings Program 2 Requirements 1. Name your program cpalindrome.cpp 2. All strings in the program are implemented as C-strings 3. The
Program 2: Palindromes With C-Strings
Program 2 Requirements
1. Name your program cpalindrome.cpp
2. All strings in the program are implemented as C-strings
3. The program accepts a potential palindrome on the command line
a. The program does not prompt for nor read input from cin
b. The test input will NOT contain punctuation characters (periods, commas, exclamation or question marks), etc.
c. The test input will consist of only one case (i.e., it will not contain a mix of upper and lower case letters)
d. The command line processor removes all spaces from the test input and places each word in a separate element of argv (see "Parsed Command Line" below)
e. Concatenate all of the needed elements of argv together to form the candidate palindrome (see "Concatenating C-Strings" below)
f. See the command line version of the 8.2.5. name_box and 8.6.1. pyramid programs for demonstrations of both accessing command line arguments and of entering command line arguments in Visual Studio
g. Test the string to see if it is a palindrome (follow either of the two examples in 8.2.7. cpalnumber.cpp)
4. The program writes a message stating that the input was or was not a valid palindrome
5. The program terminates after printing the message - no prompts or pauses
The Parsed Command Line
The system parses each word entered on the command line and stores it in a separate c-string that is one element of the argv array. For example, if the "panama" palindrome is entered on the command line (all lower case with punctuation except spaces removed), the resulting argv array would look like the following (note that the following illustrates how the input is stored in argv and argc, not something that you program):
argc = 8 argv[0] = cpalindrome (the program name - often not of interest and also not entered in VS) argv[1] = a argv[2] = man argv[3] = a argv[4] = plan argv[5] = a argv[6] = canal argv[7] = panama
Concatenating C-Strings
Concatenate the command line arguments into a single c-string:
char palindrome[1000] = ""; // empty string for (int i = 1; i < argc; i++) strcat_s(palindrome, 1000, argv[i]);
About strcat_s
strcat_s is a Microsoft variation of strcat and will not be available on other platforms. If you are working on Linux or Mac, then you have two options:
1. Use the standard concatenation function strcat and add #define _CRT_SECURE_NO_DEPRECATE at the top of your file (this is also unique to Microsoft but will not cause any problems on other platforms)
2. Use strcat during program development and replace it with strcat_s prior to uploading your code
3. Choice 1 is also a viable option if you are working on Windows with Visual Studio
Instructions for setting "command line" arguments in Visual Studio:
1.Select "Project" from the main menu OR in the Solution Explorer, right-click the project name
2. Select "Properties" (at the bottom of the pop up menu
3. Expand "Configuration Properties" (on the left) and select "Debugging
4. Enter the arguments separated by spaces (do not include the program name) in the input box labeled "Command Arguments" and Press "OK"
Using the Linux/Mac Command Line
If you are developing your code on a Linux platform, write and compile your program as you normally would. Open a shell or terminal window and cd to your executable. If you named the output with the -o option (or if you are using an IDE that automatically names the executable file, then replace "a.out" with the appropriate program name). From the command line, execute your program and specify the input:
./a.out a man a plan a canal panama
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started