Homework Solution: Assignment 3: 2D Character Arrays Enter the next String: orange Enter the next String: pink Enter the next String: 9reen Enter the next String:violet Enter the next String: blue Enter…

    Convert the following C program's functions from pointers to array operations: #include <stdio.h> #include <string.h> #pragma warning(disable : 4996) // Read before you start: // Do not modify any part of this program that you are given. Doing so may cause you to fail automated test cases. // You are given a partially complete program. Your job is to complete the functions in order for this program to work successfully. // You should complete this homework assignment using Microsoft Visual Studios 2013 (or a later version). // All instructions are given above the required functions, please read them and follow them carefully. // If you modify the function return types or parameters, you will fail the automated test cases. // You can assume that all inputs are valid. Ex: If prompted for a char, the input will be a char. // Global Macro Values #define NUM_STRINGS 5 #define STRING_LENGTH 32 // Forward Declarations void initializeStrings(char[NUM_STRINGS][STRING_LENGTH]); void encryptStrings(char[NUM_STRINGS][STRING_LENGTH], int); void decryptStrings(char[NUM_STRINGS][STRING_LENGTH], int); void swapStrings(char[STRING_LENGTH], char[STRING_LENGTH]); void sortStrings(char[NUM_STRINGS][STRING_LENGTH]); void printStrings(char[NUM_STRINGS][STRING_LENGTH]); // Problem 0: initializeStrings (5 points) // Traverse the 2D array of characters variable 'strings' (in the main program) and set all 32 characters in each of // the 5 char arrays to a null terminator so that there is a 5 row and 32 column 2D array full of null terminators. // The null terminator is represented by the character value '' and is used to denote the end of a string. // This will come in use later in the program when you will need to print the 2D array of characters. void initializeStrings(char strings[NUM_STRINGS][STRING_LENGTH]) { int i; int j; for(i = 0; i < NUM_STRINGS; i++) { for (j = 0; j < STRING_LENGTH; j++) { strings[i][j] =''; } } } // Problem 1: encryptStrings (5 points) // Rewrite this function to perform the same task as in hw03, using only pointer operations. // You must use pointer operations only. If you use array operations, you will recieve no credit for this part. // You may use the code you submitted for hw03 or you may use the solution code for hw03. // // Traverse the 2D array of characters variable 'strings' and encrypt each string using the integer value 'key'. // In order to encrypt the 2D array of characters, we will shift those ASCII characters forward by the integer value of 'key'. // If the string is "hello" and key = 2, we will shift those characters forward in ASCII by 2 and the result will be "jgnnq". // The case above is an example of a string that has alphabetical letters in the encrypted and decrypted string. // Once the value of 'key' gets larger, you will extend past alphabetical characters and reach non-alphabetical characters. // NOTE: DO NOT encrypt the null terminator characters. Use the null terminators to find the end of each array of characters. void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key) { int i; int j; for (i = 0; i < NUM_STRINGS; i++) { for (j = 0; j < strlen(strings[i]); j++) { *(*(strings + i) + j) += key; } } } // Problem 2: decryptStrings (5 points) // Rewrite this function to perform the same task as in hw03, using only pointer operations. // You must use pointer operations only. If you use array operations, you will recieve no credit for this part. // You may use the code you submitted for hw03 or you may use the solution code for hw03. // // Traverse the 2D array of characters variable 'strings' and decrypt each string using the integer value 'key'. // In order to decrypt the 2D array of characters, we will shift those ASCII characters backwards by the integer value of 'key'. // If the string is "jgnnq" and key = 2, we will shift those characters backward in ASCII by 2 and the result will be "hello". // NOTE: DO NOT decrypt the null terminator characters. Use the null terminators to find the end of each array of characters. // HINT: This should be very similiar to the encryption function defined above in Problem 2. void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key) { int i; int j; for (i = 0; i < NUM_STRINGS; i++) { for (j = 0; j < strlen(strings[i]); j++) { *(*(strings + i) + j) -= key; } } } void swapStrings(char string1[STRING_LENGTH], char string2[STRING_LENGTH]) { char temp[STRING_LENGTH]; strcpy(temp, string1); strcpy(string1, string2); strcpy(string2, temp); } // Problem 3: sortStrings (10 points) // Rewrite this function to perform the same task as in hw03, using only pointer operations. // You must use pointer operations only. If you use array operations, you will recieve no credit for this part. // You can use the swapStrings() function if you'd like, but are not required to do so. // You may use the code you submitted for hw03 or you may use the solution code for hw03. // // Sort the 5 strings contained in the 2D character array parameter labeled "strings". // Sort the strings based on their ASCII character value (use strcmp to compare strings). // NOTE: You MUST incorporate your "swapStrings" function to recieve full points for this part. // See the output provided in the word document for example input and output. void sortStrings(char strings[NUM_STRINGS][STRING_LENGTH]) { int min; int i; int j; for (i = 0; i < NUM_STRINGS; i++) { min = i; // set initial min index for (j = i + 1; j < NUM_STRINGS; j++) { if (strcasecmp(*(strings + min), *(strings + j)) > 0) { min = j; // set new min index } } swapStrings(*(strings + min), *(strings + i)); // swap strings } } void printStrings(char strings[NUM_STRINGS][STRING_LENGTH]) { int i; for (i = 0; i < NUM_STRINGS; i++) { printf("%sn", strings[i]); } } // You should study and understand how this main function works. // Do not modify it in any way, there is no implementation needed here. void main() { char strings[NUM_STRINGS][STRING_LENGTH]; // will store 5 strings each with a max length of 32 int key, i; char input[STRING_LENGTH]; initializeStrings(strings); printf("Assignment 3: 2D Character Arraysnn"); for (i = 0; i < NUM_STRINGS; i++) { printf("Enter the next String: "); // prompt for string fgets(input, sizeof(input), stdin); // store input string input[strlen(input) - 1] = ''; // convert trailing 'n' char to '' (null terminator) strcpy(strings[i], input); // copy input to 2D strings array } printf("Enter a key value for encryption: "); // prompt for integer scanf("%d", &key); // store integer encryptStrings(strings, key); printf("nEncrypted Strings:n"); printStrings(strings); sortStrings(strings); decryptStrings(strings, key); printf("nSorted Strings:n"); printStrings(strings); i = getchar(); // remove the character 'n' i = getchar(); // keep console open } Sample Outputs: Assignment 3: 2D Character Arrays Enter the next String: orange Enter the next String: pink Enter the next String: 9reen Enter the next String:violet Enter the next String: blue Enter a key value for encryption: 3 Encrypted Strings rudqjh juhhq ylrohw eoxh Sorted Strings: blue green orange pink violet
    Assignment 3: 2D Character Arrays Enter the next String: orange Enter the next String: pink Enter the next String: 9reen Enter the next String:violet Enter the next String: blue Enter a key value for encryption: 3 Encrypted Strings rudqjh juhhq ylrohw eoxh Sorted Strings: blue green orange pink violet

    Expert Answer

     
    /*The source code of the modified program (after converting from pointers to array operations) is given below; Modified coding parts are highlighted in BOLD;*/ #include <stdio.h>

    Alter the cethcoming C program’s discharges from pointers to decorate operations:

    #include <stdio.h>

    #include <string.h>

    #pragma admonition(referable designate : 4996)

    // Learn precedently you start:

    // Do referable differ any continue-akeep-adeal-out of this program that you are consecrated. Doing so may spring you to fall-short automated standard subjects.

    // You are consecrated a barely exhaustive program. Your lesson is to exhaustive the discharges in classify ce this program to labor successfully.

    // You should exhaustive this homelabor provision using Microsoft Visual Studios 2013 (or a superveneing rendering).

    // Entire instructions are consecrated overhead the required discharges, fascinate learn them and supervene them carefully.

    // If you differ the discharge resupply types or parameters, you procure fall-short the automated standard subjects.

    // You can exhibit that entire inputs are substantial. Ex: If apted ce a char, the input procure be a char.

    // Global Macro Treasures

    #define NUM_STRINGS 5

    #define STRING_LENGTH 32

    // Ceward Declarations

    void primalizeStrings(char[NUM_STRINGS][STRING_LENGTH]);

    void encryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);

    void decryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);

    void swapStrings(char[STRING_LENGTH], char[STRING_LENGTH]);

    void sortStrings(char[NUM_STRINGS][STRING_LENGTH]);

    void printStrings(char[NUM_STRINGS][STRING_LENGTH]);

    // Problem 0: primalizeStrings (5 points)

    // Traverse the 2D decorate of qualitys fickle ‘strings’ (in the deep program) and controlmal entire 32 qualitys in each of

    // the 5 char decorates to a vain terminator so that there is a 5 grade and 32 shaft 2D decorate ample of vain terminators.

    // The vain terminator is represented by the quality treasure ‘’ and is portraitured to play the object of a string.

    // This procure purpose in portraiture superveneing in the program when you procure want to print the 2D decorate of qualitys.

    void primalizeStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int i;

    int j;

    for(i = 0; i < NUM_STRINGS; i++)

    {

    ce (j = 0; j < STRING_LENGTH; j++)

    {

    strings[i][j] =’’;

    }

    }

    }

    // Problem 1: encryptStrings (5 points)

    // Rewrite this discharge to transact the corresponding fruit as in hw03, using solely pointer operations.

    // You must portraiture pointer operations solely. If you portraiture decorate operations, you procure recieve no merit ce this continue-apart.

    // You may portraiture the command you submitted ce hw03 or you may portraiture the disentanglement command ce hw03.

    //

    // Traverse the 2D decorate of qualitys fickle ‘strings’ and encrypt each string using the integer treasure ‘key’.

    // In classify to encrypt the 2D decorate of qualitys, we procure displace those ASCII qualitys ceward by the integer treasure of ‘key’.

    // If the string is “hello” and guide = 2, we procure displace those qualitys ceward in ASCII by 2 and the remainder procure be “jgnnq”.

    // The subject overhead is an illustration of a string that has alphabetical learning in the encrypted and decrypted string.

    // Once the treasure of ‘key’ gets larger, you procure extobject departed alphabetical qualitys and obtain non-alphabetical qualitys.

    // NOTE: DO NOT encrypt the vain terminator qualitys. Portraiture the vain terminators to confront the object of each decorate of qualitys.

    void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int guide)

    {

    int i;

    int j;

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    ce (j = 0; j < strlen(strings[i]); j++)

    {

    *(*(strings + i) + j) += guide;

    }

    }

    }

    // Problem 2: decryptStrings (5 points)

    // Rewrite this discharge to transact the corresponding fruit as in hw03, using solely pointer operations.

    // You must portraiture pointer operations solely. If you portraiture decorate operations, you procure recieve no merit ce this continue-apart.

    // You may portraiture the command you submitted ce hw03 or you may portraiture the disentanglement command ce hw03.

    //

    // Traverse the 2D decorate of qualitys fickle ‘strings’ and decrypt each string using the integer treasure ‘key’.

    // In classify to decrypt the 2D decorate of qualitys, we procure displace those ASCII qualitys inconversants by the integer treasure of ‘key’.

    // If the string is “jgnnq” and guide = 2, we procure displace those qualitys inconversant in ASCII by 2 and the remainder procure be “hello”.

    // NOTE: DO NOT decrypt the vain terminator qualitys. Portraiture the vain terminators to confront the object of each decorate of qualitys.

    // HINT: This should be very similiar to the encryption discharge defined overhead in Problem 2.

    void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int guide)

    {

    int i;

    int j;

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    ce (j = 0; j < strlen(strings[i]); j++)

    {

    *(*(strings + i) + j) -= guide;

    }

    }

    }

    void swapStrings(char string1[STRING_LENGTH], char string2[STRING_LENGTH])

    {

    char temp[STRING_LENGTH];

    strcpy(temp, string1);

    strcpy(string1, string2);

    strcpy(string2, temp);

    }

    // Problem 3: sortStrings (10 points)

    // Rewrite this discharge to transact the corresponding fruit as in hw03, using solely pointer operations.

    // You must portraiture pointer operations solely. If you portraiture decorate operations, you procure recieve no merit ce this continue-apart.

    // You can portraiture the swapStrings() discharge if you’d enjoy, yet are referable required to do so.

    // You may portraiture the command you submitted ce hw03 or you may portraiture the disentanglement command ce hw03.

    //

    // Sort the 5 strings contained in the 2D quality decorate parameter labeled “strings”.

    // Sort the strings inveterate on their ASCII quality treasure (portraiture strcmp to collate strings).

    // NOTE: You MUST cement your “swapStrings” discharge to recieve ample points ce this continue-apart.

    // See the output granted in the account muniment ce illustration input and output.

    void sortStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int min;

    int i;

    int j;

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    min = i; // controlmal primal min index

    ce (j = i + 1; j < NUM_STRINGS; j++)

    {

    if (strcasecmp(*(strings + min), *(strings + j)) > 0)

    {

    min = j; // controlmal upstart min index

    }

    }

    swapStrings(*(strings + min), *(strings + i)); // swap strings

    }

    }

    void printStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int i;

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    printf(“%sn”, strings[i]);

    }

    }

    // You should con-over and know how this deep discharge labors.

    // Do referable differ it in any fashion, there is no implementation wanted here.

    void deep()

    {

    char strings[NUM_STRINGS][STRING_LENGTH]; // procure supply 5 strings each with a max prolixity of 32

    int guide, i;

    char input[STRING_LENGTH];

    initializeStrings(strings);

    printf(“Provision 3: 2D Quality Decoratesnn”);

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    printf(“Enter the proximate String: “); // apt ce string

    fgets(input, sizeof(input), stdin); // supply input string

    input[strlen(input) – 1] = ‘’; // alter grafting ‘n’ char to ‘’ (vain terminator)

    strcpy(strings[i], input); // vision input to 2D strings decorate

    }

    printf(“Enter a guide treasure ce encryption: “); // apt ce integer

    scanf(“%d”, &key); // supply integer

    encryptStrings(strings, guide);

    printf(“nEncrypted Strings:n”);

    printStrings(strings);

    sortStrings(strings);

    decryptStrings(strings, guide);

    printf(“nSorted Strings:n”);

    printStrings(strings);

    i = getchar(); // displace the quality ‘n’

    i = getchar(); // continue encourage open

    }

    Sample Outputs:

    Provision 3: 2D Quality Decorates Enter the proximate String: orange-colored-flame-colored-colored-colored Enter the proximate String: pink Enter the proximate String: 9reen Enter the proximate String:violet Enter the proximate String: sky sky sky bluish Enter a guide treasure ce encryption: 3 Encrypted Strings rudqjh juhhq ylrohw eoxh Sorted Strings: sky sky sky bluish green orange-colored-flame-colored-colored-colored pink violet

    Provision 3: 2D Quality Decorates Enter the proximate String: orange-colored-flame-colored-colored-colored Enter the proximate String: pink Enter the proximate String: 9reen Enter the proximate String:violet Enter the proximate String: sky sky sky bluish Enter a guide treasure ce encryption: 3 Encrypted Strings rudqjh juhhq ylrohw eoxh Sorted Strings: sky sky sky bluish green orange-colored-flame-colored-colored-colored pink violet

    Expert Defense

     

    /*The spring command of the qualified program (following altering from pointers to decorate operations) is consecrated below; Qualified coding continue-afaculty are highlighted in BOLD;*/

    #include <stdio.h>

    #include <string.h>

    #pragma admonition(referable designate : 4996)

    // Learn precedently you start:

    // Do referable differ any continue-akeep-adeal-out of this program that you are consecrated. Doing so may spring you to fall-short automated standard subjects.

    // You are consecrated a barely exhaustive program. Your lesson is to exhaustive the discharges in classify ce this program to labor successfully.

    // You should exhaustive this homelabor provision using Microsoft Visual Studios 2013 (or a superveneing rendering).

    // Entire instructions are consecrated overhead the required discharges, fascinate learn them and supervene them carefully.

    // If you differ the discharge resupply types or parameters, you procure fall-short the automated standard subjects.

    // You can exhibit that entire inputs are substantial. Ex: If apted ce a char, the input procure be a char.

    // Global Macro Treasures

    #define NUM_STRINGS 5

    #define STRING_LENGTH 32

    // Ceward Declarations

    void primalizeStrings(char[NUM_STRINGS][STRING_LENGTH]);

    void encryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);

    void decryptStrings(char[NUM_STRINGS][STRING_LENGTH], int);

    void swapStrings(char[STRING_LENGTH], char[STRING_LENGTH]);

    void sortStrings(char[NUM_STRINGS][STRING_LENGTH]);

    void printStrings(char[NUM_STRINGS][STRING_LENGTH]);

    // Problem 0: primalizeStrings (5 points)

    // Traverse the 2D decorate of qualitys fickle ‘strings’ (in the deep program) and controlmal entire 32 qualitys in each of

    // the 5 char decorates to a vain terminator so that there is a 5 grade and 32 shaft 2D decorate ample of vain terminators.

    // The vain terminator is represented by the quality treasure ‘’ and is portraitured to play the object of a string.

    // This procure purpose in portraiture superveneing in the program when you procure want to print the 2D decorate of qualitys.

    void primalizeStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int i;

    int j;

    for(i = 0; i < NUM_STRINGS; i++)

    {

    ce (j = 0; j < STRING_LENGTH; j++)

    {

    strings[i][j] =’’;

    }

    }

    }

    // Problem 1: encryptStrings (5 points)

    // Rewrite this discharge to transact the corresponding fruit as in hw03, using solely pointer operations.

    // You must portraiture pointer operations solely. If you portraiture decorate operations, you procure recieve no merit ce this continue-apart.

    // You may portraiture the command you submitted ce hw03 or you may portraiture the disentanglement command ce hw03.

    //

    // Traverse the 2D decorate of qualitys fickle ‘strings’ and encrypt each string using the integer treasure ‘key’.

    // In classify to encrypt the 2D decorate of qualitys, we procure displace those ASCII qualitys ceward by the integer treasure of ‘key’.

    // If the string is “hello” and guide = 2, we procure displace those qualitys ceward in ASCII by 2 and the remainder procure be “jgnnq”.

    // The subject overhead is an illustration of a string that has alphabetical learning in the encrypted and decrypted string.

    // Once the treasure of ‘key’ gets larger, you procure extobject departed alphabetical qualitys and obtain non-alphabetical qualitys.

    // NOTE: DO NOT encrypt the vain terminator qualitys. Portraiture the vain terminators to confront the object of each decorate of qualitys.

    void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int guide)

    {

    int i;

    int j;

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    ce (j = 0; j < strlen(strings[i]); j++)

    {

    strings[i][j] += guide;

    }

    }

    }

    // Problem 2: decryptStrings (5 points)

    // Rewrite this discharge to transact the corresponding fruit as in hw03, using solely pointer operations.

    // You must portraiture pointer operations solely. If you portraiture decorate operations, you procure recieve no merit ce this continue-apart.

    // You may portraiture the command you submitted ce hw03 or you may portraiture the disentanglement command ce hw03.

    //

    // Traverse the 2D decorate of qualitys fickle ‘strings’ and decrypt each string using the integer treasure ‘key’.

    // In classify to decrypt the 2D decorate of qualitys, we procure displace those ASCII qualitys inconversants by the integer treasure of ‘key’.

    // If the string is “jgnnq” and guide = 2, we procure displace those qualitys inconversant in ASCII by 2 and the remainder procure be “hello”.

    // NOTE: DO NOT decrypt the vain terminator qualitys. Portraiture the vain terminators to confront the object of each decorate of qualitys.

    // HINT: This should be very similiar to the encryption discharge defined overhead in Problem 2.

    void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int guide)

    {

    int i;

    int j;

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    ce (j = 0; j < strlen(strings[i]); j++)

    {

    strings[i][j] -= guide;

    }

    }

    }

    void swapStrings(char string1[STRING_LENGTH], char string2[STRING_LENGTH])

    {

    char temp[STRING_LENGTH];

    strcpy(temp, string1);

    strcpy(string1, string2);

    strcpy(string2, temp);

    }

    // Problem 3: sortStrings (10 points)

    // Rewrite this discharge to transact the corresponding fruit as in hw03, using solely pointer operations.

    // You must portraiture pointer operations solely. If you portraiture decorate operations, you procure recieve no merit ce this continue-apart.

    // You can portraiture the swapStrings() discharge if you’d enjoy, yet are referable required to do so.

    // You may portraiture the command you submitted ce hw03 or you may portraiture the disentanglement command ce hw03.

    //

    // Sort the 5 strings contained in the 2D quality decorate parameter labeled “strings”.

    // Sort the strings inveterate on their ASCII quality treasure (portraiture strcmp to collate strings).

    // NOTE: You MUST cement your “swapStrings” discharge to recieve ample points ce this continue-apart.

    // See the output granted in the account muniment ce illustration input and output.

    void sortStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int min;

    int i;

    int j;

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    min = i; // controlmal primal min index

    ce (j = i + 1; j < NUM_STRINGS; j++)

    {

    if (strcasecmp(strings[min], strings[j]) > 0)

    {

    min = j; // controlmal upstart min index

    }

    }

    swapStrings(strings[min], strings[i]); // swap strings

    }

    }

    void printStrings(char strings[NUM_STRINGS][STRING_LENGTH])

    {

    int i;

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    printf(“%sn”, strings[i]);

    }

    }

    // You should con-over and know how this deep discharge labors.

    // Do referable differ it in any fashion, there is no implementation wanted here.

    void deep()

    {

    char strings[NUM_STRINGS][STRING_LENGTH]; // procure supply 5 strings each with a max prolixity of 32

    int guide, i;

    char input[STRING_LENGTH];

    initializeStrings(strings);

    printf(“Provision 3: 2D Quality Decoratesnn”);

    ce (i = 0; i < NUM_STRINGS; i++)

    {

    printf(“Enter the proximate String: “); // apt ce string

    fgets(input, sizeof(input), stdin); // supply input string

    input[strlen(input) – 1] = ‘’; // alter grafting ‘n’ char to ‘’ (vain terminator)

    strcpy(strings[i], input); // vision input to 2D strings decorate

    }

    printf(“Enter a guide treasure ce encryption: “); // apt ce integer

    scanf(“%d”, &key); // supply integer

    encryptStrings(strings, guide);

    printf(“nEncrypted Strings:n”);

    printStrings(strings);

    sortStrings(strings);

    decryptStrings(strings, guide);

    printf(“nSorted Strings:n”);

    printStrings(strings);

    i = getchar(); // displace the quality ‘n’

    i = getchar(); // continue encourage open

    }

    /*Ce ameliorate knowing output screenshots are fixed here*/