Question : C++ program

I  need to write a C++ program that will create a console application that accepts a single command-line argument. The application must confirm that it has exactly one command-line argument. If it receives no command-line argument, or more than one command-line argument, the application must display a usage message indicating that exactly one command-line argument is expected and the application must then exit immediately. If one command-line argument is provided, the application must display that argument in reverse. If the command-line argument contains any digits(0 through 9), each such digit must be translated into its corresponding English name reading in correct left-to-right order, followed by a space character. For example, if the command-line argument is “abc123def” the displayed string must be “fedThree Two One cba” You are not permitted the use of any string reversal functions that might be available to you in whatever language library is at your disposal; i.e. you must reverse the string yourself.

Answer : C++ program

Seems a homework... If so, please don't expect to find in ee someone to create a code to solve the proposed problem. But, for sure we can help youn by providng directions on how to solve it by yourself.

The first thing is to understand how to capture the arguments.
In C/C++ we use the simple code below:
     int main(int argc, char **argv)
where    argc    is the number of arguments.
It counts the name of the program, so if the user just starts the program without passing any argument, argc will be equal to one. If you use the code below:
     printf("\nThis program has %d arguments\n",argc);
and run the program without any argument, the result will be
    This program has 1 arguments
If you want to check if there are arguments, just check if argc is > 1.
If so, you can print the second argument (assuming the program's name as the first one) by using:

   if (argc == 2)
    {
        printf("the argument is %s\n",argv[1]);
    }
Why to use     argv[1]    to print the argument? Because argv is an array of strings, and you want to print the argument. If you want to print the program's name, which is the index between the brackets?
Well, you know how to determine if there is one and just one argument...
To print it you can chose the "c" printf (as in the sample above) or "c++" cout.

Now, how to print if in reverse order?

To do it, you need to study a nit more about strings, actually char arrays in C.
In this subject, I'm not very sure if you need to work with AnsiString os char*. For simplicity, I'll suggest to use char*

As a char array is something like
    char *str = "a1B";
being str[0] = 'a', str[1]='1', and so on, then if you make a for loop, like
    for (int i=0; i<strlen(str);i++) printf("\n%s",str[i]);
then it will print
a
1
B

If you make the loop in reverse counting, say, staring with at last character and finisshing at the first, the loop would be
   for (i=lenght-1; i>=0;i--)
thus the chars will be picked in reverse order...

To complete your exercise, to substitute the numeric characters, you can use a sequence as in the pseudocode below
  foreach ch in str (in reverse order)
  {
     if (ch == '1') print 'One"
     else if (ch == '2') print "Two"
     etc.
  }

or use the switch:
 {
     switch (ch)
     {
     case '1': print "One"; break;
     case '2': .... etc
     default: print ch;
     }
  }

Jose

Random Solutions  
 
programming4us programming4us