Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are given a C file hw04q1.c which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You

You are given a C file hw04q1.c which contains a partially completed program. Follow the instructions contained in comments and complete the required functions. You will be rewriting four functions from HW03 (initializeStrings, printStrings, encryptStrings, decryptStrings) using only pointer operations instead of using array operations. In addition to this, you will be writing two new functions (printReversedString, isValidPassword). You should not be using any array operations in any of functions for this assignment. You may use only the strlen() function from string.h library. You can find out more about these functions by reading through the instructions in the hw04q1.c file.

//CSE240 Fall 2018 HW4 // Enter your name here // State the IDE that you use: Visual Studio or GCC #include  #include  #pragma warning(disable : 4996) // compiler directive for Visual Studio only // Read before you start: // You are given a partially complete program. Your job is to complete the functions in order for this program to work successfully. // All instructions are given above the required functions, please read them and follow them carefully. // You shoud not modify the function return types or parameters. // You can assume that all inputs are valid. Ex: If prompted for an integer, the user will input an integer. // You can use only the strlen() of strings.h library to check ctring length. Do not use any other string functions // because you are supposed to use pointers for this homework. // DO NOT use arrays to store or to index the characters in the string // Global Macro Values. They are used to define the size of 2D array of characters #define NUM_STRINGS 4 #define STRING_LENGTH 35 // Forward Declarations void initializeStrings(char[NUM_STRINGS][STRING_LENGTH]); void printStrings(char[NUM_STRINGS][STRING_LENGTH]); void encryptStrings(char[NUM_STRINGS][STRING_LENGTH], int); void decryptStrings(char[NUM_STRINGS][STRING_LENGTH], int); void printReversedString(char s[STRING_LENGTH]); int isValidPassword(char s[STRING_LENGTH]); // Problem 1: initializeStrings (5 points) // Use pointer p to traverse the 2D array of characters variable 'strings' (input from user in main() ) and set all characters in each // array to a null terminator so that there is a 4 row and 35 column 2D array full of null terminators. // The null terminator is represented by the character value '\0' and is used to denote the end of a string. void initializeStrings(char strings[NUM_STRINGS][STRING_LENGTH]) { char *ptr = &strings[0][0]; } // Problem 2: printStrings (5 points) // Use pointer p to traverse the 2D character array "strings" and print each of the contained strings. // See the example outputs provided in the word document. Each string should be printed on a new line. void printStrings(char strings[NUM_STRINGS][STRING_LENGTH]) { char *ptr = &strings[0][0]; } // Problem 3: encryptStrings (5 points) // Use pointer ptr to traverse the 2D character array 'strings' and encrypt each string in 1 step as follows- // 1) Shift the 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". // Once the value of 'key' gets larger, you will extend past alphabetical characters and reach non-alphabetical characters. Thats ok. // NOTE: DO NOT encrypt the null terminator character. Use the null terminators to find the end string. void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key) { char *ptr = &strings[0][0]; } // Problem 4: decryptStrings (5 points) // HINT: This should be very similiar to the encryption function defined above in Problem 3. // Use pointer ptr to traverse the 2D character array 'strings' and decrypt each string in 1 step as follows- // 1)Shift the characters backward by the integer value of 'key'. // NOTE: DO NOT decrypt the null characters. void decryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key) { char *ptr = &strings[0][0]; } // Problem 5: reverseStrings (15 points) // Reverse the string s and print it, by using pointers. // Use pointer p and 'temp' char to swap 1st char with last, then 2nd char with (last-1) and so on.. // Finally print the reversed string at the end of this function // Hint: You might want to check if your logic works with even as well as odd length string. void printReversedString(char s[STRING_LENGTH]) { char temp; // not necessary to use this variable char *p = &s[0]; // pointer to start of string } // Problem 6: isValidPassword (15 points) // Return 1 if the password satisfies the requirements, else return 0. // Password requirements: atleast 5 characters long, should contain atleast one lower case char, atleast one uppercase char, // atleast one number and only numbers and letters. // Valid password examples: Asu123, Cse240, abCd9. Invalid password examples: ASU123, Cidse, Asu1 // Traverse through the string by using pointer p to check if all conditions are satisfied // Note that the password string contains char at the end when you press 'Enter' to enter the password. int isValidPassword(char s[STRING_LENGTH]) { char *p = &s[0]; // enter code here return 0; // remove this line when implementing this function. // it is added initially , so that the empty function does not give compile error. } // You should study and understand how this main() works. // *** DO NOT modify it in any way *** int main() { char strings[NUM_STRINGS][STRING_LENGTH]; // will store four strings each with a max length of 34 int i, key; char input[STRING_LENGTH]; printf("CSE240 HW4: Pointers "); initializeStrings(strings); for (i = 0; i < NUM_STRINGS; i++) { printf("Enter a string: "); // prompt for string fgets(input, sizeof(input), stdin); // store input string input[strlen(input) - 1] = '\0'; // convert trailing ' ' char to '\0' (null terminator) strcpy(strings[i], input); // copy input to 2D strings array } printf(" Enter a key value for encryption: "); // prompt for integer key scanf("%d", &key); encryptStrings(strings, key); printf(" Encrypted Strings: "); printStrings(strings); decryptStrings(strings, key); printf(" Decrypted Strings: "); printStrings(strings); getchar(); // flush out newline ' ' char printf(" Enter a string to reverse: "); // prompt for string fgets(input, sizeof(input), stdin); // store input string printReversedString(input); getchar(); // flush out newline ' ' char printf(" A password should be atleast 5 char long and should contain atleast one lower case char, atleast one uppercase char,"); printf(" atleast one number and only numbers and letters"); printf(" Enter a password to validate: "); // prompt for string fgets(input, sizeof(input), stdin); // store input string if(isValidPassword(input)) printf(" Password is valid"); else printf(" Password is NOT valid "); getchar(); // keep console open return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago