Question

In: Computer Science

Make a modification of the program below, that will read in the string as a “command-line...

Make a modification of the program below, that will read in the string as a “command-line argument” to your program, instead of having the user type it while your program is running.

Your program should print out the inverted string to the screen.

#include <iostream>

#include <cstring>

using namespace std;

int Reverse(char * destination, const char * source, int num);

int main() // this is the test/driver code, for your function

{

const int STRINGSIZE = 10;

char oldCString[] = "Hello!";

char newCString[STRINGSIZE];

cout << "oldCString: " << oldCString << endl;

cout << "newCString before changing: " << oldCString << endl;

Reverse(newCString, oldCString, STRINGSIZE); // testing your function...

cout << "newCString after Reverse: " << newCString << endl;

return 0;

}

// Reverses a C-string passed in (source), and places the reversed

// C-string into (destination).

// (num) should represent the maximum valid length of (destination).

// If no null-zero character is found in (source) within (num-1)

// characters,the function will only read up to (num-1) characters

// from (source), and then copy the reversed characters to

// (destination) and append a null-zero to the end of it.

// The function will return the number of characters placed into

// (destination), including the null-zero.

// The function MUST use pointer notation (not array notation) inside

// it. However, you might find it useful to use array notation

// temporarily while developing the function, and then replace

// with pointer notation before turning in the assignment.

int Reverse(char * destination, const char * source, int num)

{

for (int x = 0; x < strlen(source); x++)

*( x + destination) = *( strlen(source) + source - 1 - x);

*(destination + strlen(source)) = '\0';

return 0;

}

For example, if you are running your program in the command window (by clicking the Start button, then typing in “cmd”, then pressing Enter, then using the “cd ” command to change to the appropriate directory where the compiler has placed your .exe executable file), a successful run might look like this:

M:\CS1113\Lab9>lab9.exe "This is a test"

tset a si sihT

If the user does not enter the string argument, your program should print out a brief “proper usage” message, which explains to the user the proper syntax of running your program, and which arguments to pass in. For example,

M:\CS1113\Lab9>lab9.exe

Welcome to our Lab 9 program which reverses a string!

To use this program, please use the following syntax:

      lab9.exe “input string”

M:\CS1113\Lab9>

Here is some sample code which reads in arguments from the command line, and just prints them out (taken from the example at http://www.cplusplus.com/forum/beginner/5404/#msg23839   )

#include <iomanip>

#include <iostream>

using namespace std;

int main( int argc, char* argv[] )

{

cout << "The name used to start the program: " << argv[ 0 ]

      << "\nArguments are:\n";

for (int n = 1; n < argc; n++)

    cout << setw( 2 ) << n << ": " << argv[ n ] << '\n';

return 0;

}

You may encounter errors in passing in the arguments.   If you are using the command window (cmd.exe) on your computer (not the remote server) to run the program, make sure you build your program in “Release” mode, rather than “Debug” mode as we have typically done. This changes the default .dll libraries used by the program to the ones included in Windows.

Solutions

Expert Solution

#include <iomanip>
#include <iostream>
#include <cstring>
using namespace std;
int Reverse(char * destination,  char * source, int num);
int main( int argc, char* argv[] )
{
    int STRINGSIZE = 10;
    char newCString[STRINGSIZE];
    
cout << "The name used to start the program: " << argv[ 0 ]
      << "\nArguments are:\n";
for (int n = 1; n < argc; n++)
    cout << setw( 2 ) << n << ": " << argv[ n ] << '\n';


cout << "oldCString: " << argv[1] << endl;

cout << "newCString before changing: " << argv[1] << endl;

Reverse(newCString, argv[1], STRINGSIZE); // testing your function...

cout << "newCString after Reverse: " << newCString << endl;


return 0;
}

int Reverse(char * destination,  char * source, int num)
{
for (int x = 0; x < strlen(source); x++)
*( x + destination) = *( strlen(source) + source - 1 - x);

*(destination + strlen(source)) = '\0';

return 0;
}

Output:


Related Solutions

Your team will make a modification of the program #1 above, that will read in the...
Your team will make a modification of the program #1 above, that will read in the string as a “command-line argument” to your program, instead ofhaving the user type it while your program is running. Your program should print out the inverted string to the screen. For example, if you are running your program in the command window (by clicking the Start button, then typing in “cmd”, then pressing Enter, then using the “cd ” command to change to the...
Use C++ to write a program that reads in a binary string from the command line...
Use C++ to write a program that reads in a binary string from the command line and applies the following (00, 1101) tag-system: if the first bit is 0, deletes the first three bits and append 00; if the first bit is 1, delete the first three bits and append 1101. Repeat as long as the string has at least 3 bits. Try to determine whether the following inputs will halt or go into an infinite loop: 10010, 100100100100100100. Use...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename...
program c Write a program called filesearch that accepts two command-line arguments: A string A filename If the user did not supply both arguments, the program should display an error message and exit. The program opens the given filename. Each line that contains the given string is displayed. Use the strstr function to search each line for the string. You may assume no line is longer than 255 characters. The matching lines are displayed to standard output (normally the screen).
Make a program to read a file, filled with words separated by new lines, from command...
Make a program to read a file, filled with words separated by new lines, from command line arguments (args[0]), print the file, and then add each word into an arraylist. After making the arraylist, iterate through it and print its contents.
Write a program Median.java to read each file whose name is specified in the command-line arguments....
Write a program Median.java to read each file whose name is specified in the command-line arguments. That is, for each command-line argument, open it as a file and read it. The file contents are zero or more lines each containing a list of comma-separated integers, such as 1,2,3,4 or 99,120,33. You should parse each of these integers and save them in an ArrayList (if you prefer you may use an array, but an ArrayList is likely to be easier for...
B. Write a program Median.java to read each file whose name is specified in the command-line...
B. Write a program Median.java to read each file whose name is specified in the command-line arguments. That is, for each command-line argument, open it as a file and read it. The file contents are zero or more lines each containing a list of comma-separated integers, such as 1,2,3,4 or 99,120,33. You should parse each of these integers and save them in an ArrayList (if you prefer you may use an array, but an ArrayList is likely to be easier...
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command...
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command line and prints every permutation of that string. You may assume the string will contain all unique characters. You may print the permutations in any order, as long as you print them all. ---------------- Output: ---------------- $>./prog dog d do dog dg dgo o od odg og ogd g gd gdo go god ---------------- I need help on this exercise that requires recursion only....
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command...
---------------- Exercise 2: String Permutations ---------------- Create a program that takes a string from the command line and prints every permutation of that string. You may assume the string will contain all unique characters. You may print the permutations in any order, as long as you print them all. ---------------- Output: ---------------- $>./prog dog d do dog dg dgo o od odg og ogd g gd gdo go god ---------------- I have no idea on coding this. The files I...
Python programming: Instructions: The python program should respond to user input on a command line Below...
Python programming: Instructions: The python program should respond to user input on a command line Below are the four options - if the user input is A, the program will quit -if the user input is B, the program will display the number of times the prompt has been displayed -if the user input is C, will display whatever is stored. -if the user inputs something else, it will store that user input as a string and append it to...
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT