//CSE240 Fall 2018 HW4 // Enter your name here // State the IDE that you use: Vi
ID: 3748844 • 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;
int i,j;
for(i=0;i<NUM_STRINGS;i++)
{
for(j=0;j<STRING_LENGTH;j++ )
{
ptr= &strings[i][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;
int i;
for(i=0;i<NUM_STRINGS;i++)
{
ptr= &strings[i];
printf("String %d:%s ",i+1,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;
int i,j;
for(i=0;i<NUM_STRINGS;i++)
{
for(j=0;j<STRING_LENGTH;j++ )
{
ptr= &strings[i][j];
if((*ptr)!='')
{
*ptr=(*ptr)+key;
}
else
{
break;
}
}
}
}
// 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;
int i,j;
for(i=0;i<NUM_STRINGS;i++)
{
for(j=0;j<STRING_LENGTH;j++ )
{
ptr= &strings[i][j];
if((*ptr)!='')
{
*ptr=(*ptr)-key;
}
else
{
break;
}
}
}
}
// 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])
{
int i=0,j=strlen(s)-2;
char temp; // not necessary to use this variable
char *p,*q; // pointer to start of string
printf(" Orignal String:%s",s);
for(i=0;i<strlen(s)&&i<j;i++)
{
p = &s[i];
q = &s[j];
temp=*p;
*p=*q;
*q=temp;
j--;
}
printf(" Reverse String:%s",s);
}
// 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;
int i=0;
int Upr_flag=0,Lwr_flag=0,Num_flag=0;
if(strlen(s)<=5)
{
return 0;
}
while(i<strlen(s)-1)
{
p = &s[i];
if((*p)>='a'&&(*p)<='z')
{
Lwr_flag=1;
}
else if((*p)>='A'&&(*p)<='Z')
{
Upr_flag=1;
}
else if((*p)>='0'&&(*p)<='9')
{
Num_flag=1;
}
else
{
return 0;
}
i++;
}
return Upr_flag*Lwr_flag*Num_flag;
}
// 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);
printf(" ");
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.