C programming I need to write a second function that takes i

C programming:

I need to write a second function that takes in a second command line argument (Unix: gcc (program.c) and ./a.out) that takes in only a string of uppercase hexadecimal between 3 and 40 characters in a string and print them out. Then I need a function that will convert those hexadecimal characters into binary and print them out. If the amount of characters entered are not correct then the program will prompt that the user entered the incorrect input and exit. Here is my code thuse far. The first command line argument picks a mode.

//Libraries

#include <stdio.h>

#include <stdlib.h>

//Functional Prototypes

void modeType(char *argv);

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

  

  

//Invalid command line arguments

if(argc != 3) {

printf(\"Did not enter the correct arguments.\ \");

fflush(stdout);

exit(0);

}

  

//First command line input

printf(\"Mode of operation: \");

fflush(stdout);

modeType(argv[1]);

  

  

return 0;

}

//Function for first command line argument

void modeType(char *argv) {

  

if(*argv == \'v\') {

printf(\"validation\ \");

fflush(stdout);

}

else if(*argv == \'c\') {

printf(\"calculation\ \");

fflush(stdout);

}

else {

printf(\"Invalid command line arguments.\ \");

fflush(stdout);

exit(0);

}

}

Solution

//Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Functional Prototypes

#define MAX 1000

char hexa[MAX];
long int i = 0;

char *con (char a){
        char * ans = (char *)malloc(4 * sizeof(char));
            switch (a)
            {
            case \'0\':
                strcpy(ans, \"0000\"); return ans;
            case \'1\':
                strcpy(ans, \"0001\"); return ans;
            case \'2\':
                strcpy(ans, \"0010\"); return ans;
            case \'3\':
                strcpy(ans, \"0011\"); return ans;
            case \'4\':
                strcpy(ans, \"0100\"); return ans;
            case \'5\':
                strcpy(ans, \"0101\"); return ans;
            case \'6\':
                strcpy(ans, \"0110\"); return ans;
            case \'7\':
                strcpy(ans, \"0111\"); return ans;
            case \'8\':
                strcpy(ans, \"1000\"); return ans;
            case \'9\':
                strcpy(ans, \"1001\"); return ans;
            case \'A\':
                strcpy(ans, \"1010\"); return ans;
            case \'B\':
                strcpy(ans, \"1011\"); return ans;
            case \'C\':
                strcpy(ans, \"1100\"); return ans;
            case \'D\':
                strcpy(ans, \"1101\"); return ans;
            case \'E\':
                strcpy(ans, \"1110\"); return ans;
            case \'F\':
                strcpy(ans, \"1111\"); return ans;
            case \'a\':
                strcpy(ans, \"1010\"); return ans;
            case \'b\':
                strcpy(ans, \"1011\"); return ans;
            case \'c\':
                strcpy(ans, \"1100\"); return ans;
            case \'d\':
                strcpy(ans, \"1101\"); return ans;
            case \'e\':
                strcpy(ans, \"1110\"); return ans;
            case \'f\':
                strcpy(ans, \"1111\"); return ans;
            default:
                return ans;
            }
      
}

void modeType(char *argv);
void conve(char *argv);

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

    //Invalid command line arguments

    if(argc != 3) {
        printf(\"Did not enter the correct arguments.\ \");
        fflush(stdout);
        exit(0);
    }
    //First command line input
    printf(\"Mode of operation: \");
    fflush(stdout);
    modeType(argv[1]);

    //Second command line input
    printf(\"Mode of operation: \");
    fflush(stdout);
    conve(argv[2]);
    return 0;
}

//Function for first command line argument
void modeType(char *argv) {
    if(*argv == \'v\') {
        printf(\"validation\ \");
        fflush(stdout);
    }
    else if(*argv == \'c\') {
        printf(\"calculation\ \");
        fflush(stdout);
    }
  
    else {
        printf(\"Invalid command line arguments.\ \");
        fflush(stdout);
        exit(0);
    }
}
void conve (char *argv) {
    int len = ((strlen(argv) - 2) << 2);
    char * ans = (char *)malloc(len * sizeof(char) + 1);
    for(int i = 0; i < len; i++) ans[i] = \'0\';
    argv += 2;

    char last = 0;
    for(int i = len; i > 3; i -= 4){
        strcpy(ans + i - 4, con(argv[i / 4 - 1]));
        ans[i] = last;
        last = ans[i - 4];
    }
    printf(\"%s\ \", ans);
}

C programming: I need to write a second function that takes in a second command line argument (Unix: gcc (program.c) and ./a.out) that takes in only a string of
C programming: I need to write a second function that takes in a second command line argument (Unix: gcc (program.c) and ./a.out) that takes in only a string of
C programming: I need to write a second function that takes in a second command line argument (Unix: gcc (program.c) and ./a.out) that takes in only a string of
C programming: I need to write a second function that takes in a second command line argument (Unix: gcc (program.c) and ./a.out) that takes in only a string of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site