Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//CSE240 Fall 2018 HW4 // Enter your name here // State the IDE that you use: Vi

ID: 3748658 • Letter: #

Question

 //CSE240 Fall 2018 HW4  // Enter your name here // State the IDE that you use: Visual Studio or GCC  #include <stdio.h> #include <string.h>  #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 '' 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] = '';                // convert trailing ' ' char to '' (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; }

Explanation / Answer

//CSE240 Fall 2018 HW4

// Enter your name here

// State the IDE that you use: Visual Studio or GCC

#include <stdio.h>

#include <string.h>

#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 '' and is used to denote the end of a string.

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

{

char *ptr = &strings[0][0];

int i,j;

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

{

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

{

*ptr++ = '';

}

}

  

}

// 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];

int i,j;

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

{

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

{

printf("%c",*ptr++);

}

}

}

// 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];

char *movingPointer = &strings[0][0];

int i;

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

while(*movingPointer != '') {

*movingPointer = *movingPointer + key;

movingPointer++;

}

ptr = ptr + 35;

movingPointer = ptr;

}

  

}

// 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];

char *movingPointer = &strings[0][0];

int j = 0;

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

while (*movingPointer != 0) {

*movingPointer = *movingPointer - key;

movingPointer++;

}

ptr = ptr + 35;

movingPointer = ptr;

j = 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])

{ // not necessary to use this variable

char *p = &s[0]; // pointer to start of string

  

int length, c;

length=strlen(s);

length--;

char *end, temp;

end=p;

for (c = 0; c < length-1; c++)

end++;

for (c = 0; c < length/2; c++)

{   

temp = *end;

*end = *p;

*p = temp;

p++;

end--;

}

int i;

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

printf("%c",s[i]);

}

// 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];

char c;

int len,i,flag1=0,flag2=0,flag3=0,flag4=0,flag5=0;

len=strlen(s);

if(len<6)

flag1=1;

else

{

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

if((s[i]>=48&&s[i]<=57))

{

flag2=0;

break;

}

else

flag2=1;

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

if((s[i]>=65&&s[i]<=90))

{

flag3=0;

break;

}

else

flag3=1;

  

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

if((s[i]>=97&&s[i]<=122))

{

flag4=0;

break;

}

else

flag4=1;

  

for(i=0;i<len-1;i++)

if( (s[i]>=48&&s[i]<=57) || (s[i]>=65&&s[i]<=90) || (s[i]>=97&&s[i]<=122))

{

flag5=0;

}

else

{

flag5=1;

break;

}

  

if(flag1==1||flag2==1||flag3==1||flag4==1 || flag5==1)

{

return 0;   

}

return 1;

}

}

// 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];

// 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;

}