I need some help with my code for C++ using putty. The asignment is: Complete th
ID: 3783037 • Letter: I
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 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 <iostream>
#include <cstring>
#include <cctype>
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" <<endl;
}
if(argv[3] >argv[1]){
std::cerr << "Usage: " << "the rear index must be less than the length of"<< argv[1] <<endl;
}
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.
Explanation / Answer
//main.cpp
#include <iostream>
using namespace std;
// Reverse Function
void reverse(char *front, char *rear){
char holder;
int length = rear - front;
for (int i = 0; i < (length/2 +1); i++){
holder = *rear;
*rear = *front;
*front = holder;
front++;
rear--;
}
}
int main(int argc, const char * argv[]) {
//retrieve the command line parameters and store in variables
string sentence = argv[1];
string strFront = argv[2];
string strRear = argv[3];
//convert the strings into integers
int intFront = atoi(strFront.c_str());
int intRear = atoi(strRear.c_str());
//convert the integers to addresses
char *frontPt = &sentence[intFront];
char *rearPt = &sentence[intRear];
//if the first number is not an actual number
if (intFront == 0 && strFront.compare("0") != 0) {
//check if the second number is not an actual number either
if (intRear == 0 && strRear.compare("0") != 0){
cout << "Usage: " << strFront << " and " << strRear << " must be integers ";
}
//if the second number is a real number then only the first number is wrong
else{
cout << "Usage: " << strFront << " must be an integer ";
}
return 0; //exit the program
}//end if
//if the second number is not an actual number
if (intRear == 0 && strRear.compare("0") != 0) {
cout << "Usage: " << strRear << " must be an integer ";
return 0; //exit the program
}//end if
//make sure the numbers are non-negative
if (intFront < 0 || intFront > intRear) {
cout << "Usage: The front index must be non-negative and less than or equal to the rear index (i.e., less than or equal to " << intRear << ") ";
return 0;
}
if (intRear > (sentence.length()-1)) {
cout << "Usage: The rear index must be less than the length of "" << sentence << "" (i.e., less than " << sentence.length() << ") ";
return 0;
}
//******************** Reverse the String **************************
cout << "Reversing "" << sentence << "" from position " << intFront << " to position " << intRear << " ";
reverse(frontPt, rearPt);
cout << "yields "" << sentence << "" ";
return 0;
}