Question: I need some help with my code for C++ using putty. The asignment is: Complete the Chapter 9 Programming Project 4. NOTE: complete this assignment
I need some help with my code for C++ using putty.
The asignment is: Complete the Chapter 9 Programming Project 4. NOTE: complete this assignment with the following modifications:
The function to reverse the C string must use the following signature: void reverse(char *front, char *rear);
In addition to the reverse() funtion, you are to write a main() program that:
Uses command line arguments to specify the values for str, frontIndex, and rearIndex (see examples of the program's execution below for details)
Ensures front and rear indices are valid numbers, i.e., they are:
Actual numbers,
The value of frontIndex is greater than or equal to 0 AND less than or equal to rearIndex,
The value of the rearIndex is less than the length of str.
Please note, the front and rear indices are specified on the command line as zero-based, integer positions. However, these positions (which originally are numeric ASCII literals, e.g., "4") must be first converted to integers (e.g., 4) and then converted to character addresses (e.g., &str[4]) which reference the characters of str at those indicies. All these conversions occur BEFORE main() calls the reverse() function.
Examples: - Sample Executions: In the executions below, the text in bold represents that which is typed in by the user of the program. pp9.4 "This is a test" dog cat Usage: dog and cat must be integers pp9.4 "This is a test" -1 4 Usage: the front index must be non-negative and less than or equal to the rear index (i.e., less than or equal to 4) pp9.4 "This is a test" 0 14 Usage: the rear index must be less than the length of "This is a test" (i.e., less than 14) pp9.4 "This is a test" 0 13 Reversing "This is a test" from position 0 to position 13 yields "tset a si sihT"
Note that the user should be able to input whatever he wishes not just "this is a test."
Chapter 9 programming project 4 says:
Write a function that takes a C string as an input parameter and reverses the string. The function should usetwo pointers front and rear. Thefront pointer should initially reference the firstcharacter in the string, and the rear pointer shouldinitially reference the last character in the string. Reverse the string by swapping the characters referenced by front andrear, then increment front to point to thenext character and decrement rear to point to the preceding character, and so on, until the entire string is reversed. Write a main program function to test your function on various strings of both even and odd length.
This is my code so far.
#include
using namespace std;
int main(int argc, char* argv[]) { const char * foo = argv[1]; if (argc < 3) { std::cerr << "Usage: " << argv[0] << "STR FRONTINDEX REARINDEX" << std::endl; return 1; } if(isdigit(argv[2],argv[3]){ return 1; }else{ std::cerr << "Usage: " << argv[2] << "and"<< argv[3] << "must be integers" << std::endl; } if(argv[2] <0||argv[2] >=argv[3]){ std::cerr << "Usage: " << argv[2] << "must be non-negative and less than or equal to the rear index" < return reverse (argv[2], argv[3]); } void reverse(char *front, char *rear){ if ( rear <= 1 || !front ) { return; } std::swap(s[0], s[len-1]); s++; // move pointer to the following char reverse(s, len-2); } I am struggling with converting the indecies to numbers and then converting those numbers to pointers and I'm not sure if my code for reversing is correct.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
